在JAR中加载.txt文件

时间:2014-12-24 03:15:27

标签: java properties fileinputstream

我的包db.properties.txt

com.noteu个文件

在与我正在尝试将其加载到Database类中的属性对象的构造函数中的文件相同的包中,如下所示:

properties.load(Database.class.getResourceAsStream("db.properties.txt"));

但是我得到java.lang.NullPointerException如下:

Exception in thread "main" java.lang.NullPointerException
    at java.util.Properties$LineReader.readLine(Unknown Source)
    at java.util.Properties.load0(Unknown Source)
    at java.util.Properties.load(Unknown Source)
    at com.noteu.Database.get(Database.java:24)
    at com.noteu.menus.Signin.checkSignedInStatus(Signin.java:52)
    at com.noteu.menus.Signin.<init>(Signin.java:253)
    at com.noteu.Main.main(Main.java:33)

1 个答案:

答案 0 :(得分:1)

将文件类型更改为.propertiesproperties.load();properties文件加载txt文件作为流。

public void load(InputStream inStream) throws IOException

从输入字节流中读取属性列表(键和元素对)。

修改后的代码:

properties.load(Database.class.getResourceAsStream("db.properties"));

要加载.txt文件而不是属性文件,您必须创建FileInputStream实例。

FileInputStream fis new FileInputStream("myfile.txt");
props.load(fis)
  

如果我的解决方案无效,请确保您的路径是   正确的。

相关问题