【问题标题】:getResourceAsStream() is returning null. Properties file is not loadinggetResourceAsStream() 返回 null。属性文件未加载
【发布时间】:2013-08-05 07:48:35
【问题描述】:

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

现在我正在尝试加载 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

这个程序打印

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

但它没有从该路径加载属性文件。虽然它在那里。为什么我得到 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)

【问题讨论】:

    标签: java


    【解决方案1】:

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

    1) 在您提供的第一个代码 sn-p 中,您使用 ClassLoader 加载资源文件。这确实是一个不错的决定。但是getResourceAsStream 方法需要一个“类路径相对”名称。您提供的是绝对路径。

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

    我担心,您的描述与您机器上的发现不是最新的。

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

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

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

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

    编辑

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

    【讨论】:

    • 是的,我发现我将test.proeprties 文件复制到了错误的位置。现在它在将其放在正确的位置后从流中加载:)。是的,我正在使用 Maven。您的意思是将属性文件移动到src/main/resources 文件夹。并按您所说的将src/main/resources 文件夹添加到类路径(第2点)?
    • 我认为 src/main/resources 已经在 maven 中的类路径上......?
    • @Basit:没错。 “src/main/resources”是此类资源的 Maven 默认目录。如果您使用 Maven 打包您的应用程序,它将被自动考虑(并成为您的 JAR 的一部分)。如果你在 Eclipse 中运行项目,M2E 插件会处理这个问题。
    • 是的,它在放入src/main/resources 后正在加载。制作jar时我想做的一件事然后将属性文件包含在jar中,我不想将它包含在jar中,我已经使用maven完成了它,但是在将它从jar中移动后我卡住了我怎么能指示我的 jar 读取属性文件。如果我打开我的 jar,那么我的属性文件位于 jar 的根目录下。
    • 非常好的解释,我正在尝试使用Thread.currentThread().getContextClassLoader().getResourceAsStream("myfile.properties") 它给了我null。您能否像上面的答案一样解释这种行为和幕后操作。
    【解决方案2】:

    请将您的属性文件放在 /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();
    }
    

    【讨论】:

    • 好的,放入资源文件夹后,只需使用InputStream resourceAsStream = BatchImport.class.getResourceAsStream("test.properties");。是吗?
    • 是的,因为 ClassLoader 在类路径中搜索文件
    • 经过无数次尝试,终于让它工作了,记得先清理并构建它才会更新正确的路径
    【解决方案3】:

    您使用的是类加载器(在类路径中读取),而您使用的是绝对路径。

    试试吧:

    InputStream resourceAsStream =  new FileInputStream(temp);
    

    作为旁注,请尝试实例化您的文件:

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

    使用系统相关的路径分隔符。

    【讨论】:

    • 我遇到了异常。找不到文件异常为什么?在这里,我使用了这个InputStream resourceAsStream = new FileInputStream(temp);。属性文件在那里。您可以在输出中看到它...?
    • 实际上,您的workingDir 不是包含属性的那个!您的属性位于LS360AutomatedRegulatorsReportingService,而代码返回LS360BatchImportIntegration
    • 是的,我知道了,现在它正在加载:)。实际上我正在制作jar,我希望在制作jar之后,属性文件不包含在jar中,否则即使进行测试我也需要再次创建jar。所以我希望我把我的属性文件放在一个地方,在制作 jar 之后我可以指出我的 jar 从那里读取属性文件。我正在使用 maven 创建 jar。
    【解决方案4】:

    getResourceAsStream() 找不到一个文件,我遇到了类似的问题。该文件位于资源文件夹 (src/main/resources) 中,但仍未找到。

    当我进入 Eclipse 包资源管理器并“刷新”资源文件夹时,问题得到了解决。它在目录中,但 Eclipse 直到刷新文件夹后才看到它(右键单击文件夹并选择 Refresh)。

    【讨论】:

      【解决方案5】:

      我希望这会有所帮助! 您可以将 test.properties 保存到 src/main/resources

       public static Properties props = new Properties();
          InputStream inStream = Test.class.getResourceAsStream("/test.properties");
          try {
                  loadConfigurations(inStream);
              } catch (IOException ex) {
                  String errMsg = "Exception in loading configuration file. Please check if application.properties file is present in classpath.";
                  ExceptionUtils.throwRuntimeException(errMsg, ex, LOGGER);
              }
          public static void loadConfigurations(InputStream inputStream) throws IOException{
                  props.load(inputStream);
              }
      

      【讨论】:

        【解决方案6】:

        您将文件路径传递给getResourceAsStream(String name),但name 这里是类路径,而不是文件路径...

        您可以确保该文件位于您的类路径中,或者改用 FileInputStream

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-19
          • 1970-01-01
          • 2011-07-11
          • 2013-10-02
          • 2013-06-21
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多