在我的JAR构建完成后,我试图读取“资源”文件夹的内容。在IDE设置(IntelliJ)中,资源文件夹被标记为“源”。
我尝试了以下方法:
InputStream input = getClass().getResourceAsStream("../objectLocation.json");
JsonReader jsonReader = new JsonReader(new InputStreamReader(input));
我还尝试过以下方法:
JsonReader jsonReader = new JsonReader(new FileReader("../resources/objectLocation.json"));
这两种方法都会导致以下结果:
这将导致:
java.io.FileNotFoundException: com/layers/resources/objectLocation.json (No such file or directory)
文件结构:
src
-com.layers -> myClasses
-resources -> JSON
编辑:
InputStream input = getClass().getResourceAsStream("objectLocation.json");
JsonReader jsonReader = new JsonReader(new InputStreamReader(input));
结果如下:
java.lang.NullPointerException
InputStream input = getClass().getResourceAsStream("objectLocation.json");
,如果它位于资源文件夹的子文件夹中,则使用InputStream input = getClass().getResourceAsStream("<subfolder>/objectLocation.json");
- MrTjava.lang.NullPointerException
。 - Colin747try .getResourceAsStream("/objectLocation.json");
- MrT