在java中加载.properties文件的问题

时间:2010-10-05 19:34:04

标签: java

我在加载.properties文件时遇到问题。 以下作品:

private static Properties props = new Properties();
 ................
 .......................
 configurationProps.load(Test.class.getResourceAsStream("test.properties"));

但是这个;

private static Properties props = new Properties();
 ................
 .......................
 configurationProps.load(Test.class.getResourceAsStream("C:\\someFilder\\test.properties"));

给出以下错误。

Exception in thread "main" java.lang.NullPointerException
 at java.util.Properties.load(Properties.java:267)
 at Test.init(Test.java:24)
 at Test.main(Test.java:16)

我想知道为什么它没有走完全路。 任何建议都受到高度赞赏。

2 个答案:

答案 0 :(得分:5)

尝试

configurationProps.load(new FileInputStream("C:\\..."));

使用getResourceAsStream委托给ClassLoader,因此通常仅相对于类路径工作。其搜索位置的规则在Javadoc for getResource()

中给出

答案 1 :(得分:-2)

private static Properties props = new Properties();
 ................
 .......................
 configurationProps.load(Test.class.getResourceAsStream("../../../../../../../../../../../../../../../../../../../../C:\\someFilder\\test.properties"));