java 绝对路径_java中获取绝对路径的几种方式

2023-05-16

// 第一种:获取类加载的根路径

File f = new File(this.getClass().getResource("/").getPath());

// 获取当前类的所在工程路径; 如果不加“/” 获取当前类的加载目录

File f2 = new File(this.getClass().getResource("").getPath());

// 第二种:获取项目路径

File directory = new File("");// 参数为空

String courseFile = directory.getCanonicalPath();

// 第三种:

URL xmlpath = this.getClass().getClassLoader().getResource("");

// 第四种:

System.out.println(System.getProperty("user.dir"));

// 第五种:

System.out.println(System.getProperty("java.class.path"));

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

java 绝对路径_java中获取绝对路径的几种方式 的相关文章

随机推荐