Java 中的绝对文件路径与相对文件路径
鉴于我有两个 File
对象,我可以想到以下实现:
public File convertToRelative(File home, File file) {
final String homePath = home.getAbsolutePath();
final String filePath = file.getAbsolutePath();
// Only interested in converting file path that is a
// direct descendants of home path
if (!filePath.beginsWith(homePath)) {
return file;
}
return new File(filePath.substring(homePath.length()+1));
}
是否有一些更智能的方法将绝对文件路径转换为相对文件路径?
可能的重复:
Given I have two File
objects I can think of the following implementation:
public File convertToRelative(File home, File file) {
final String homePath = home.getAbsolutePath();
final String filePath = file.getAbsolutePath();
// Only interested in converting file path that is a
// direct descendants of home path
if (!filePath.beginsWith(homePath)) {
return file;
}
return new File(filePath.substring(homePath.length()+1));
}
Is there some smarter way of converting an absolute file path to a relative file path?
Possible Duplicate:
How to construct a relative path in java from two absolute paths or urls
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题之前被问过 这里
这是答案,以防万一
This question was asked before here
Here is the answer just in case