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

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

目录结构

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

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

谢谢

编辑— —————————-

 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.(Unknown Source) at com.softech.ls360.integration.BatchImport.main(BatchImport.java:57) 

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

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”。 如果是这样,请使用它。

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

只需尝试:

 InputStream resourceAsStream = new FileInputStream(temp); 

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

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

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

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

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

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

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

当我进入eclipse Package Explorer并“刷新”资源文件夹时,问题得到了解决。 它位于目录中,但是在刷新文件夹之前Eclipse没有看到它(右键单击该文件夹并选择Refresh)。