对于Java文件路径,网上看的很多,但未必对自身的系统正确,唯有自己实践出真知。
立此存照,以备候查。环境jdk1.8.0-66.
public class MyClass {
public static void main(String[] args) {
System.out.println("getClass().getResource(\"\"): " + MyClass.class.getClass().getResource(""));
System.out.println("getClass().getResource(\"/\"): " + MyClass.class.getClass().getResource("/"));
System.out.println("getClassLoader().getResource(\"\"): " + MyClass.class.getClassLoader().getResource(""));
System.out.println("getClassLoader().getResource(\"/\"): " + MyClass.class.getClassLoader().getResource("/"));
System.out.println("ClassLoader.getSystemResource(\"\"): " + ClassLoader.getSystemResource(""));
System.out.println("ClassLoader.getSystemResource(\"/\"): " + ClassLoader.getSystemResource("/"));
System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"\"): " + Thread.currentThread().getContextClassLoader().getResource(""));
System.out.println("Thread.currentThread().getContextClassLoader().getResource(\"/\"): " + Thread.currentThread().getContextClassLoader().getResource("/"));
File f1 = new File("");
System.out.println("File(\"\").getPath: " + f1.getPath());
System.out.println("File(\"\").getAbsolutePath: " + f1.getAbsolutePath());
try {
System.out.println("File(\"\").getCanonicalPath: " + f1.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("File(\"\").toURI: " + f1.toURI());
File f2 = new File(".");
System.out.println("File(\".\").getPath: " + f2.getPath());
System.out.println("File(\".\").getAbsolutePath: " + f2.getAbsolutePath());
try {
System.out.println("File(\".\").getCanonicalPath: " + f2.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("File(\".\").toURI: " + f2.toURI());
File f3 = new File("..");
System.out.println("File(\"..\").getPath: " + f3.getPath());
System.out.println("File(\"..\").getAbsolutePath: " + f3.getAbsolutePath());
try {
System.out.println("File(\"..\").getCanonicalPath: " + f3.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("File(\"..\").toURI: " + f3.toURI());
}
}
得到结果:
getClass().getResource(""): null
getClass().getResource("/"): file:/D:/sts-64/workspaces/tsPath/bin/
getClassLoader().getResource(""): file:/D:/sts-64/workspaces/tsPath/bin/
getClassLoader().getResource("/"): null
ClassLoader.getSystemResource(""): file:/D:/sts-64/workspaces/tsPath/bin/
ClassLoader.getSystemResource("/"): null
Thread.currentThread().getContextClassLoader().getResource(""): file:/D:/sts-64/workspaces/tsPath/bin/
Thread.currentThread().getContextClassLoader().getResource("/"): null
File("").getPath:
File("").getAbsolutePath: D:\sts-64\workspaces\tsPath
File("").getCanonicalPath: D:\sts-64\workspaces\tsPath
File("").toURI: file:/D:/sts-64/workspaces/tsPath/
File(".").getPath: .
File(".").getAbsolutePath: D:\sts-64\workspaces\tsPath\.
File(".").getCanonicalPath: D:\sts-64\workspaces\tsPath
File(".").toURI: file:/D:/sts-64/workspaces/tsPath/./
File("..").getPath: ..
File("..").getAbsolutePath: D:\sts-64\workspaces\tsPath\..
File("..").getCanonicalPath: D:\sts-64\workspaces
File("..").toURI: file:/D:/sts-64/workspaces/tsPath/../