getResourceAsStream()返回null.属性文件未加载

Bas*_*sit 20 java

我正在尝试加载属性文件.这是我的结构

目录结构

现在我正在尝试加载test.properties文件.但是我变得无效了.我在这里怎么样

public class Test {

    String workingDir = System.getProperty("user.dir");
    System.out.println("Current working directory : " + workingDir);

    File temp = new File(workingDir + "\\" + "test.properties");
    String absolutePath = temp.getAbsolutePath();
    System.out.println("File path : " + absolutePath);

    Properties properties = null;

    try {
        properties = new Properties();
        InputStream resourceAsStream =  Test.class.getClassLoader().getResourceAsStream(absolutePath);
        if (resourceAsStream != null) {
            properties.load(resourceAsStream);
        }


    } catch (IOException e) {
        e.printStackTrace();
    }

    System.exit(0);

} //end of class Test
Run Code Online (Sandbox Code Playgroud)

这个程序打印

Current working directory : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration
File path : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties
Run Code Online (Sandbox Code Playgroud)

但它不是从此路径加载属性文件.虽然它存在于那里.为什么我会变空?

谢谢

编辑--- ----------------------------

String workingDir = System.getProperty("user.dir");
System.out.println("Current working directory : " + workingDir);

File temp = new File(workingDir, "test.properties");

String absolutePath = temp.getAbsolutePath();
System.out.println("File path : " + absolutePath);

try {
    properties = new Properties();
    InputStream resourceAsStream =  new FileInputStream(temp);
    if (resourceAsStream != null) {
        properties.load(resourceAsStream);
    }   
} catch (IOException e) {
    e.printStackTrace();
}

System.exit(0);

Current working directory : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration
File path : D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties
java.io.FileNotFoundException: D:\Personal Work\eclipse 32 Bit\workspace\Spring Integration\LS360BatchImportIntegration\test.properties (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at com.softech.ls360.integration.BatchImport.main(BatchImport.java:57)
Run Code Online (Sandbox Code Playgroud)

See*_*ose 26

哦哦......这里有几个问题:

1)在您提供的第一个代码段中,您使用的ClassLoader是加载资源文件.这确实是一个很好的决定.但该getResourceAsStream方法需要"类路径相对"名称.您正在提供绝对路径.

2)您的第二个代码段(编辑后)导致无法找到文件"D:...\LS360BatchImportIntegration\test.properties".根据您的截图,该文件应为"D:...\LS360AutomatedRegulatorsReportingService\test.properties".这是另一个目录.

我担心,您的描述与您机器上的调查结果不符.

但是,让我们转向一个合理的解决方案:

1)在Eclipse项目中(截图告诉我们,您正在使用Eclipse),创建一个名为"resources"的新目录,其深度与"src"目录相同.复制 - 或更好地移动 - 属性文件到其中.

2)必须将此新目录放入"构建路径".在Package Explorer或Project Explorer视图中右键单击该目录,选择"Build Path",然后选择"Use as Source Folder".注意:当您运行项目时,此构建路径将是项目的类路径.

3)由于资源目录现在是类路径的一部分并包含属性文件,因此您只需加载它即可getResourceAsStream("test.properties").

编辑

我只是看到,你也使用Maven(pom.xml文件).在Maven中,默认存在这样的资源目录,它是构建路径的一部分.它是"src/main/resources".如果是这样,请使用它.


Muk*_*med 10

请将您的属性文件放在/ src/main/resources文件夹中,然后从ClassLoader加载.它会被修复.

喜欢

 /src/main/resources/test.properties



Properties properties = null;

try {
    properties = new Properties();
    InputStream resourceAsStream =  Test.class.getClassLoader().getResourceAsStream("test.properties");
    if (resourceAsStream != null) {
        properties.load(resourceAsStream);
    }


} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)


Jea*_*art 9

您正在使用类加载器(读取类路径),而您正在使用绝对路径.

只需尝试:

InputStream resourceAsStream =  new FileInputStream(temp);
Run Code Online (Sandbox Code Playgroud)

作为旁注,请尝试将文件设置为:

File temp = new File(workingDir, "test.properties");
Run Code Online (Sandbox Code Playgroud)

使用系统相关的路径指示器.