Possible Duplicate:
How to construct a relative path in Java from two absolute paths (or URLs)?
using java, is there method to return the relative path of a file to a given folder?
There's no method included with Java to do what you want. Perhaps there's a library somewhere out there that does it (I can't think of any offhand, and Apache Commons IO doesn't appear to have it). You could use this or something like it:
// returns null if file isn't relative to folder
public static String getRelativePath(File file, File folder) {
String filePath = file.getAbsolutePath();
String folderPath = folder.getAbsolutePath();
if (filePath.startsWith(folderPath)) {
return filePath.substring(folderPath.length() + 1);
} else {
return null;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With