java获取指定资源文件路径的几种方法

时间:2022-12-09 18:43:10

java代码如下:

public class test {
public static void main(String[] args) throws URISyntaxException {


String path1 = test.class.getResource("/convertFile").getPath();
String path2 = test.class.getResource("/convertFile").toURI().getPath();
String path3 = test.class.getClassLoader().getResource("convertFile").getPath();
String path4= test.class.getClassLoader().getResource("convertFile").toURI().getPath();

System.out.println("path1:"+path1);
System.out.println("path2:"+path2);
System.out.println("path3:"+path3);
System.out.println("path4:"+path4);

}

}

输出结果如下图:
java获取指定资源文件路径的几种方法

(如有不正确,欢迎纠错)