[英]Reading Properties file in Java
我有以下代码试图读取属性文件:
Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();
InputStream stream = loader.getResourceAsStream("myProp.properties");
prop.load(stream);
我在最后一行出现异常。 具体来说:
Exception in thread "main" java.lang.NullPointerException
at java.util.Properties$LineReader.readLine(Properties.java:418)
at java.util.Properties.load0(Properties.java:337)
at java.util.Properties.load(Properties.java:325)
at Assignment1.BaseStation.readPropertyFile(BaseStation.java:46)
at Assignment1.BaseStation.main(BaseStation.java:87)
谢谢,尼科斯
根据您的异常, InputStream
为空,这意味着类加载器未找到您的属性文件。 我猜 myProp.properties 位于您项目的根目录中,如果是这种情况,您需要前面的斜杠:
InputStream stream = loader.getResourceAsStream("/myProp.properties");
您可以在此页面上找到信息:
http://www.mkyong.com/java/java-properties-file-examples/
Properties prop = new Properties();
try {
//load a properties file from class path, inside static method
prop.load(App.class.getClassLoader().getResourceAsStream("config.properties"));
//get the property value and print it out
System.out.println(prop.getProperty("database"));
System.out.println(prop.getProperty("dbuser"));
System.out.println(prop.getProperty("dbpassword"));
}
catch (IOException ex) {
ex.printStackTrace();
}
您可以使用ResourceBundle
类来读取属性文件。
ResourceBundle rb = ResourceBundle.getBundle("myProp.properties");
Properties prop = new Properties();
try {
prop.load(new FileInputStream("conf/filename.properties"));
} catch (IOException e) {
e.printStackTrace();
}
conf/filename.properties
基于项目根目录
你不能像这样使用这个关键字 -
props.load(this.getClass().getResourceAsStream("myProps.properties"));
在静态上下文中。
最好的办法是掌握应用程序上下文,例如 -
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/app-context.xml");
然后你可以从类路径加载资源文件 -
//load a properties file from class path, inside static method
prop.load(context.getClassLoader().getResourceAsStream("config.properties"));
这将适用于静态和非静态上下文,最好的部分是此属性文件可以位于应用程序类路径中包含的任何包/文件夹中。
您的文件应该在类路径中作为com/example/foo/myProps.properties
。 然后将其加载为:
props.load(this.getClass().getResourceAsStream("myProps.properties"));
如果您的 config.properties 不在 src/main/resource 目录中,而是在项目的根目录中,那么您需要执行以下操作:-
Properties prop = new Properties();
File configFile = new File(myProp.properties);
InputStream stream = new FileInputStream(configFile);
prop.load(stream);
您可以使用 java.io.InputStream 读取文件,如下所示:
InputStream inputStream = getClass().getClassLoader().getResourceAsStream(myProps.properties);
我看到这个问题是一个老问题。 如果将来有人偶然发现这一点,我认为这是一种简单的方法。 将属性文件保存在您的项目文件夹中。
FileReader reader = new FileReader("Config.properties");
Properties prop = new Properties();
prop.load(reader);
确保文件名正确并且文件实际上在类路径中。 如果这不是导致最后一行抛出异常的情况,则getResourceAsStream()
将返回 null。
如果 myProp.properties 位于项目的根目录中,请改用/myProp.properties
。
鉴于上下文loader.getResourceAsStream("myPackage/myProp.properties")
应该被使用。
前导'/'
不适用于ClassLoader.getResourceAsStream(String)
方法。
或者,您可以使用Class.getResourceAsStream(String)
方法,该方法使用'/'
来确定路径是相对于类位置的绝对路径还是相对路径。
例子:
myClass.class.getResourceAsStream("myProp.properties")
myClass.class.getResourceAsStream("/myPackage/myProp.properties")
如果您的属性文件路径和您的 java 类路径相同,那么您应该这样做。
例如:
src/myPackage/MyClass.java
src/myPackage/MyFile.properties
Properties prop = new Properties();
InputStream stream = MyClass.class.getResourceAsStream("MyFile.properties");
prop.load(stream);
这里的许多答案描述了危险的方法,它们实例化文件输入流,但没有获得对输入流的引用以便稍后关闭流。 这会导致悬空输入流和内存泄漏。 加载属性的正确方法应类似于以下内容:
Properties prop = new Properties();
try(InputStream fis = new FileInputStream("myProp.properties")) {
prop.load(fis);
}
catch(Exception e) {
System.out.println("Unable to find the specified properties file");
e.printStackTrace();
return;
}
请注意try-with-resources
块中文件输入流的实例化。 由于FileInputStream
是可自动关闭的,它会在try-with-resources
块退出后自动关闭。 如果你想使用一个简单的try
块,你必须使用fis.close();
显式关闭它fis.close();
在finally
块中。
当前的答案都没有显示InputStream
正在关闭(这将泄漏文件描述符),和/或不处理.getResourceAsStream()
在未找到资源时返回 null(这将导致NullPointerException
和令人困惑的消息, "inStream parameter is null"
)。 您需要类似以下内容:
String propertiesFilename = "server.properties";
Properties prop = new Properties();
try (var inputStream = getClass().getClassLoader().getResourceAsStream(propertiesFilename)) {
if (inputStream == null) {
throw new FileNotFoundException(propertiesFilename);
}
prop.load(inputStream);
} catch (IOException e) {
throw new RuntimeException(
"Could not read " + propertiesFilename + " resource file: " + e);
}
以原始顺序读取属性文件:
File file = new File("../config/edc.properties");
PropertiesConfiguration config = new PropertiesConfiguration();
PropertiesConfigurationLayout layout = new PropertiesConfigurationLayout(config);
layout.load(new InputStreamReader(new FileInputStream(file)));
for(Object propKey : layout.getKeys()){
PropertiesConfiguration propval = layout.getConfiguration();
String value = propval.getProperty((String) propKey).toString();
out.print("Current Key:" + propkey + "Current Value:" + propval + "<br>");
}
指定从 src 开始的路径如下:
src/main/resources/myprop.proper
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.