如何在java中缩短文件路径(获取规范路径)?

时间:2014-10-13 09:02:48

标签: java file path

java中有没有办法缩短目录的绝对路径。

例如:

./data/../system/bin/ => ./system/bin/

1 个答案:

答案 0 :(得分:5)

是的,请使用http://docs.oracle.com/javase/7/docs/api/java/io/File.html#getCanonicalPath()

File file = new File("C:/Users/../Users");
System.out.println(file.getAbsolutePath()); // C:\Users\..\Users
System.out.println(file.getCanonicalPath()); // C:\Users
相关问题