版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/weixin_43215250/article/details/90199572
-
获取类加载的根路径
File file = new File(this.getClass().getResource("/").getPath()); System.out.println(file);
结果:
D:\ProjectDemo\test-demo\target\classes
如果不加"/",则获取当前类的绝对路径
File file = new File(this.getClass().getResource("").getPath()); System.out.println(file);
结果:
D:\ProjectDemo\test-demo\target\classes\cn\cnyimi\demo
-
获取当前类的所在项目路径
File directory = new File("");// 参数为空 String courseFile = directory.getCanonicalPath(); System.out.println(courseFile);
结果:
D:\ProjectDemo
-
获取当前工程src目录下文件的路径
URL xmlpath = this.getClass().getClassLoader().getResource(""); System.out.println(xmlpath);
结果:
file:/D:/ProjectDemo/test/target/classes/
-
获取当前工程路径
System.out.println(System.getProperty("user.dir"));
结果:
D:\ProjectDemo
-
获取所有的类路径,包括jar包的路径
System.out.println(System.getProperty("java.class.path"));
测试代码:
public class MyUrlDemo {
public static void main(String[] args) {
MyUrlDemo muDemo = new MyUrlDemo();
try {
muDemo.showURL();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void showURL() throws IOException {
// 第一种: 获取类加载的根路径 D:\ProjectDemo\test\target\classes
File f1 = new File(this.getClass().getResource("/").getPath());
System.out.println(f1);
// 如果不加"/",则获取当前类的绝对路径 D:\ProjectDemo\test\target\classes\cn\cnyimi\demo
File f2 = new File(this.getClass().getResource("").getPath());
System.out.println(f2);
// 第二种: 获取当前类的所在项目路径 D:\ProjectDemo
File directory = new File("");// 参数为空
String courseFile = directory.getCanonicalPath();
System.out.println(courseFile);
// 第三种: 获取当前工程资源文件目录下文件的路径 file:/D:/ProjectDemo/test/target/classes/
URL xmlpath = this.getClass().getClassLoader().getResource("");
URL xmlpath1 = this.getClass().getClassLoader().getResource("test.txt");
System.out.println(xmlpath); // file:/D:/ProjectDemo/test/target/classes/
System.out.println(xmlpath1);// file:/D:/ProjectDemo/tesst/target/classes/test.txt
// 第四种: 获取当前工程路径 D:\ProjectDemo
System.out.println(System.getProperty("user.dir"));
// 第五种: 获取所有的类路径 包括jar包的路径
System.out.println(System.getProperty("java.class.path"));
}
}