package cn.wj.service;
import java.io.File;
import java.io.IOException;
/**
* @Author: wangjie
* @Date: 2019/9/28 17:13
*/
public class TestPath {
public static void main(String[] args) throws IOException {
String pathProperty = System.getProperty("user.dir");
System.out.println(pathProperty);
File f = new File("");
String absolutePath = f.getAbsolutePath();
System.out.println(absolutePath);
String canonicalPath = f.getCanonicalPath();
System.out.println(canonicalPath);
File f1 = new File(".");
String absolutePath1 = f1.getAbsolutePath();
System.out.println("absolutePath1:" + absolutePath1);
String canonicalPath1 = f1.getCanonicalPath();
System.out.println("canonicalPath1:" + canonicalPath1);
File f2 = new File("..");
String absolutePath2 = f2.getAbsolutePath();
System.out.println("absolutePath2:" + absolutePath2);
String canonicalPath2 = f2.getCanonicalPath();
System.out.println("canonicalPath2:" + canonicalPath2);
}
}
执行结果如下:
E:\minis
E:\minis
E:\minis
absolutePath1:E:\minis\.
canonicalPath1:E:\minis
absolutePath2:E:\minis\..
canonicalPath2:E:\
由此可以看出
1.通过System.getProperty("user.dir");//获取当前项目名
2.file.getAbsolutePath();//获取当前项目+file中的名,但是不会对../这种进行解析,也就是说不会返回上一级,单纯的就是文件路径
3.file.getcanonicalPath();//也是获取当前项目+file中的名,但是会对../这种产生解析