我在加载.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)
我想知道为什么它没有走完全路。 任何建议都受到高度赞赏。
答案 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"));