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?
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;
}
}
between File.getPath() and String.split() you should be all set to do it.
http://download.oracle.com/javase/6/docs/api/java/io/File.html#getPath%28%29
http://download.oracle.com/javase/6/docs/api/java/lang/String.html#split%28java.lang.String%29