我正在从我的Java项目的编译JAR中的包中加载一个文本文件。相关目录结构如下:

/src/initialization/Lifepaths.txt

我的代码通过调用Class::getResourceAsStream来返回一个InputStream来加载一个文件。

public class Lifepaths {
    public static void execute() {
        System.out.println(Lifepaths.class.getClass().
            getResourceAsStream("/initialization/Lifepaths.txt"));
    }

    private Lifepaths() {}

    //This is temporary; will eventually be called from outside
    public static void main(String[] args) {execute();}
}

不管我用什么,输出总是输出null。我不知道为什么上面的方法不管用,所以我也尝试了一下:

“初始化/ src / / Lifepaths.txt” “初始化/ Lifepaths.txt” “Lifepaths.txt”

这两种方法都不起作用。到目前为止,我已经阅读了许多关于这个主题的问题,但没有一个是有帮助的——通常,他们只是说使用根路径加载文件,而我已经这样做了。或者只是从当前目录加载文件(只是加载文件名),我也尝试过。该文件将被编译到JAR中的适当位置,并具有适当的名称。

我怎么解决这个问题?


当前回答

不要使用绝对路径,让它们相对于项目中的“资源”目录。快速和肮脏的代码,显示MyTest.txt的内容从目录“资源”。

@Test
public void testDefaultResource() {
    // can we see default resources
    BufferedInputStream result = (BufferedInputStream) 
         Config.class.getClassLoader().getResourceAsStream("MyTest.txt");
    byte [] b = new byte[256];
    int val = 0;
    String txt = null;
    do {
        try {
            val = result.read(b);
            if (val > 0) {
                txt += new String(b, 0, val);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    } while (val > -1);
    System.out.println(txt);
}

其他回答

如果您使用Maven,请确保您的包装是“jar”而不是“pom”。

<packaging>jar</packaging>

不要使用绝对路径,让它们相对于项目中的“资源”目录。快速和肮脏的代码,显示MyTest.txt的内容从目录“资源”。

@Test
public void testDefaultResource() {
    // can we see default resources
    BufferedInputStream result = (BufferedInputStream) 
         Config.class.getClassLoader().getResourceAsStream("MyTest.txt");
    byte [] b = new byte[256];
    int val = 0;
    String txt = null;
    do {
        try {
            val = result.read(b);
            if (val > 0) {
                txt += new String(b, 0, val);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } 
    } while (val > -1);
    System.out.println(txt);
}

我的问题是资源不在src文件夹。

确保您的资源目录(例如:"src")在类路径中(确保它是eclipse中构建路径中的源目录)。

确保clazz是从主类加载器加载的。

然后,加载src/initialization/Lifepaths.txt,使用

clazz.getResourceAsStream("/initialization/Lifepaths.txt");

原因: getresourcesasstream (foo)从clazz的类路径中查找foo,相对于clazz所在的目录。前导的“/”使它从clazz类路径中的任何目录的根目录加载。

除非您在某种类型的容器中,比如Tomcat,或者直接使用classloader做一些事情,否则您可以只将eclipse/命令行类路径视为唯一的类加载器类路径。

jdk >=9 needs to open non-root/non-co-parent packages to it in the module-info module.

JDK >=9需要在module-info.java中打开资源目录,例如:

src java resoureces 相依 json log4j2.xml openapi.yaml

如果需要读取conf/config。Json,你需要两个步骤:

// in module-info.java
module your.mod.name {
    open conf;
}

// then in java code
getClassLoader().getResourceAsStream("conf/config.json");

否则,如果你需要在根目录下读取other,它只是:

getClassLoader().getResourceAsStream("openapi.yaml");

你可以看到{@link java.lang.ClassLoader#getResourceAsStream(String)}知道为什么~