代码中获取Resource下的文件错误方式 getResource("") String path = App.class.getClassLoader().getResource("test").getPath(); file = new File(path);复制代码
运行 jar的时候就会抛出 : Exception in thread "main" java.io.FileNotFoundException: file:\E:\java8 \build\libs\java8-1.0.jar!\test (文件名、目录名 或卷标语法不正确。) at java.io.FileInputStream.open0(Native Method) at java.io.FileInputStream.open(Unknown Source) at java.io.FileInputStream.(Unknown Source) at java.io.FileReader. (Unknown Source) at App.main(App.java:13)复制代码
出现了诡异的文件路径:file:\E:\java8\build\libs\java8-1.0.jar!\test
正确方式 : getResourceAsStream("") in = App.class.getClassLoader().getResourceAsStream("test");复制代码