如何使用java获取文件的相对路径到一个文件夹 [英] How to get the relative path of the file to a folder using java
本文介绍了如何使用java获取文件的相对路径到一个文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
使用java,是否有方法返回文件的相对路径到给定的文件夹?
using java, is there method to return the relative path of a file to a given folder?
推荐答案
Java没有包含任何方法来完成您想要的任务。也许在那里有一个图书馆是这样做的(我不能想到任何副手,而Apache Commons IO似乎没有)。你可以使用它或类似的东西:
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;
}
}
这篇关于如何使用java获取文件的相对路径到一个文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文