男人勤劳家才富,女人节俭纱成布——佚名

这里

1
2
3
4
5
6
7
8
9
10
11
12
13
import java.io.File;

class Scratch {
public static void main(String[] args) throws Exception {
File file = new File("../scratch.java");
String path = file.getPath();
String absolutePath = file.getAbsolutePath();
String canonicalPath = file.getCanonicalPath();
System.out.println("path:" + path);
System.out.println("absolutePath:" + absolutePath);
System.out.println("canonicalPath:" + canonicalPath);
}
}

三种获取路径

getPath是获取构造File传入的路径

输出为:

1
path:../scratch.java

getAbsolutePath是获取绝对路径

1
absolutePath:/Users/achao/IdeaProjects/stream-query/../scratch.java

还有一个getCanonicalPath是返回相对路径

1