简书链接:java获取当前路径的坑
文章字数:40,阅读全文大约需要1分钟

1
2
3
4
5
6
7
8
9
10
11
File defaultPath =new File(System.getProperty("user.dir"));
//路径正常
System.out.println("x:"+new File("").getAbsolutePath());
//路径正常
System.out.println("x:"+new File("xx").getAbsolutePath());
//错误的方法
System.out.println("x:"+new File("xx").getParentFile());//null
// 兼容 正确
System.out.println("method1::"+new File(new File("xx").getAbsolutePath()).getParentFile());


System.setProperty(“user.dir”, defaultRootPath.getAbsolutePath());//运行时导致无法读取,权限问题还是bug?