我正在尝试读取属性文件以获取值。但是,代码抛出异常。
异常
Exception in thread "main" java.lang.ExceptionInInitializerError
at com.cisco.installbase.hiveconnector.ReadProperties.getInstance(ReadProperties.java:28)
at com.cisco.installbase.hiveconnector.MainApp.main(MainApp.java:7)
Caused by: java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:434)
at java.util.Properties.load0(Properties.java:353)
at java.util.Properties.load(Properties.java:341)
at com.cisco.installbase.hiveconnector.ReadProperties.<init>(ReadProperties.java:16)
at com.cisco.installbase.hiveconnector.ReadProperties.<init>(ReadProperties.java:12)
at com.cisco.installbase.hiveconnector.ReadProperties$PropHolder.<clinit>(ReadProperties.java:23)
... 2 more
ReadProperties.java
package com.cisco.installbase.hiveconnector;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import java.util.Set;
public class ReadProperties {
private final Properties props = new Properties();
private ReadProperties()
{
InputStream in = this.getClass().getClassLoader().getResourceAsStream("config.properties");
try{
props.load(in);
}catch(IOException e){
e.printStackTrace();
}
}
private static class PropHolder{
private static final ReadProperties INSTANCE = new ReadProperties();
}
public static ReadProperties getInstance()
{
return PropHolder.INSTANCE;
}
public String getProperty(String key)
{
return props.getProperty(key);
}
public Set<String> getAllPropertyNames()
{
return props.stringPropertyNames();
}
public boolean containsKey(String key)
{
return props.containsKey(key);
}
}
我的道具文件的目录结构和位置
有人可以帮我找到需要放置属性文件的位置。
答案 0 :(得分:3)
您的文件config.properties
不在类路径中,因此无法通过this.getClass().getClassLoader().getResourceAsStream("config.properties")
将其放在src/main/resources
答案 1 :(得分:1)
如果这是典型的maven项目,那么属性文件将在src / main / resources下。 Maven会将它移到类路径中。