也说Java文件路径

        对于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/../

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值