在 Java 中读取属性文件 [英] Reading Properties file in Java

查看:37
本文介绍了在 Java 中读取属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码试图读取属性文件:

I have the following code trying to read a properties file:

Properties prop = new Properties();
ClassLoader loader = Thread.currentThread().getContextClassLoader();           
InputStream stream = loader.getResourceAsStream("myProp.properties");
prop.load(stream);

我在最后一行出现异常.具体:

I get an exception at the last line. Specifically:

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 位于您项目的根目录中,如果是这种情况,您需要在前面加一个斜杠:

Based on your exception, the InputStream is null, this means the class loader is not finding your properties file. I'm guessing that myProp.properties is in the root of your project, if that's the case, you need a preceding slash:

InputStream stream = loader.getResourceAsStream("/myProp.properties");

这篇关于在 Java 中读取属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆