getResourceAsStream()返回null。属性文件未加载 [英] getResourceAsStream() is returning null. Properties file is not loading

查看:2329
本文介绍了getResourceAsStream()返回null。属性文件未加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to load properties file. Here is my structure

现在我正在尝试加载test.properties文件。但是我变得无效了。这是我在做什么

Now i am trying to load test.properties file. But i am getting null. Here how i am doing

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

此程序打印

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

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

But it is not loading properties file from this path. Although it is present there. Why i am getting null ?

谢谢

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

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)


推荐答案

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

Oh oh ... There are several problems here:

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

1) In your first provided code snippet, you are using a ClassLoader for loading a resource file. This is indeed a good decision. But the getResourceAsStream method needs a "class-path relative" name. You are providing an absolute path.

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

2) Your second code snippet (after edit) results in not being able to find the file "D:...\LS360BatchImportIntegration\test.properties". According to your screenshot, the file should be "D:...\LS360AutomatedRegulatorsReportingService\test.properties". This is another directory.

我担心你的描述与你机器上的调查结果不符。

I fear, that your descriptions are not up to date with the findings on your machine.

但是,让我们转到合理的解决方案:

But let's just move to a reasonable solution:

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

1) In your Eclipse project (the screenshot tells us, that you are using Eclipse), create a new directory named "resources" in the same depth as your "src" directory. Copy - or better move - the properties file into it.

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

2) This new directory must be put into the "build path". Right-click the directory in the Package Explorer or Project Explorer view, select "Build Path", then "Use as Source Folder". Note: This build path will be the class path for the project, when you run it.

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

3) As the resources directory now is part of your class path and contains your properties file, you can simply load it with getResourceAsStream("test.properties").

EDIT

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

I just see, that you also use Maven (the pom.xml file). In Maven, such a resources directory exists by default and is part of the build path. It is "src/main/resources". If so, just use this.

这篇关于getResourceAsStream()返回null。属性文件未加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆