利用System.getProperty()函数获取当前项目路径:
String currentPath=System.getProperty("user.dir"); System.out.println(currentPath);
输出结果:
当前路径+"/.."创建File对象,实际上创建的是上层路径的对象
例:currentPath="F:\code\java\text\demo"
File file=new File(currentPath+"/..");
file的绝对路径实际是"F:\code\java\text\demo\.."
其实"F:\code\java\text\demo\.."就是"F:\code\java\text"String currentPath=System.getProperty("user.dir"); System.out.println(currentPath); //当前路径+"/.."创建File对象,实际上创建的是上层路径的对象 File file=new File(currentPath+"/.."); System.out.println(file.getAbsolutePath());
"./":代表目前所在的目录。
" . ./"代表上一层目录。
"/":代表根目录。