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

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

BzrUN.png

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

This program prints

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 ?

Thanks

Edit---

----------------------------

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)

解决方案

Oh oh ... There are several problems here:

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) 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) 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) 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) 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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值