java CLASS里读取路径 各种方法比较

//第一种结果(前边有个斜线) System.out.println(Test.class.getClassLoader().getResource("a.txt").getPath()); System.out.println("===========华丽丽的分割线==========="); //第二种结果(前边以file打头) System.out.println(Thread.currentThread().getContextClassLoader().getResource("")); System.out.println(ClassLoader.getSystemResource("")); System.out.println(Test.class.getResource("")); System.out.println(Test.class.getResource("/")); System.out.println("===========华丽丽的分割线==========="); //第三种结果(定位到工程目录) System.out.println(new File("").getAbsolutePath()); System.out.println(System.getProperty("user.dir"));

 

 

结果

 

/F:/eclipse/workspace/Socket/bin/a.txt
===========华丽丽的分割线===========
file:/F:/eclipse/workspace/Socket/bin/
file:/F:/eclipse/workspace/Socket/bin/
file:/F:/eclipse/workspace/Socket/bin/
file:/F:/eclipse/workspace/Socket/bin/
===========华丽丽的分割线===========
F:/eclipse/workspace/Socket
F:/eclipse/workspace/Socket

你可能感兴趣的:(java,File,Class)