gpt4 book ai didi

java - 未找到属性文件并引发异常?

转载 作者:行者123 更新时间:2023-12-01 16:54:06 25 4
gpt4 key购买 nike

我正在尝试读取属性文件以获取值。但是,代码抛出异常。

异常

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

读取属性.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);
}
}

我的 prop 文件的目录结构和位置

enter image description here

有人可以帮我确定属性文件需要放置的位置吗?

最佳答案

您的文件config.properties不在类路径上,因此无法通过this.getClass().getClassLoader().getResourceAsStream("config.properties")加载

将其放在src/main/resources

请咨询Standard Maven directory layout

关于java - 未找到属性文件并引发异常?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35208167/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com