可能的重复:
如何在 Java 中从两个绝对路径(或 URL)构造相对路径? https://stackoverflow.com/questions/204784/how-to-construct-a-relative-path-in-java-from-two-absolute-paths-or-urls
使用java,有没有方法返回文件到给定文件夹的相对路径?
Java 中没有包含任何方法来执行您想要的操作。也许某个地方有一个库可以做到这一点(我想不出任何即兴的库,并且 Apache Commons IO 似乎没有它)。你可以使用这个或类似的东西:
// 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;
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)