用Java读取属性文件

nik*_*kos 101 java properties

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

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

我在最后一行得到一个例外.特别:

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)
Run Code Online (Sandbox Code Playgroud)

谢谢,尼科斯

Mar*_*iot 80

根据您的异常,InputStream为null,这意味着类加载器未找到您的属性文件.我猜测myProp.properties位于项目的根目录中,如果是这种情况,则需要前面的斜杠:

InputStream stream = loader.getResourceAsStream("/myProp.properties");
Run Code Online (Sandbox Code Playgroud)

  • @Mark Elliot如果我有一个`conf`包来存储我的所有配置文件,我的文件层次结构是:`myproject - > src,conf,test`,如何通过添加前面的斜杠来加载属性? (2认同)

小智 52


您可以在此页面上找到相关信息: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();
}
Run Code Online (Sandbox Code Playgroud)

  • 根据文档,不要忘记关闭prop.load(reader)中的`Reader`:`指定的流在此方法返回后保持打开状态。 (3认同)

dku*_*mar 25

您可以使用ResourceBundle类来读取属性文件.

ResourceBundle rb = ResourceBundle.getBundle("myProp.properties");
Run Code Online (Sandbox Code Playgroud)

  • i18n [推荐](/sf/ask/488489081/) 这种方法。 (2认同)

小智 10

Properties prop = new Properties();

try {
    prop.load(new FileInputStream("conf/filename.properties"));
} catch (IOException e) {
    e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)

conf/filename.properties 基于项目根目录


man*_*vsr 9

我发现这个问题是一个老问题了。如果将来有人偶然发现这一点,我认为这是一种简单的方法。将属性文件保存在项目文件夹中。

        FileReader reader = new FileReader("Config.properties");

        Properties prop = new Properties();
        prop.load(reader);
Run Code Online (Sandbox Code Playgroud)


Muk*_*kus 7

你不能使用这个关键字 -

props.load(this.getClass().getResourceAsStream("myProps.properties"));
Run Code Online (Sandbox Code Playgroud)

在静态环境中.

最好的办法是获取应用程序上下文,如 -

ApplicationContext context = new ClassPathXmlApplicationContext("classpath:/META-INF/spring/app-context.xml");
Run Code Online (Sandbox Code Playgroud)

然后你可以从类路径加载资源文件 -

//load a properties file from class path, inside static method
        prop.load(context.getClassLoader().getResourceAsStream("config.properties"));
Run Code Online (Sandbox Code Playgroud)

这适用于静态和非静态上下文,最好的部分是此属性文件可以位于应用程序类路径中包含的任何包/文件夹中.

  • ApplicationContext仅在Spring应用程序的情况下可用 (4认同)

yeg*_*256 5

您的文件应该像com/example/foo/myProps.propertiesclasspath一样可用.然后将其加载为:

props.load(this.getClass().getResourceAsStream("myProps.properties"));
Run Code Online (Sandbox Code Playgroud)


小智 5

如果您的 config.properties 不在 src/main/resource 目录中,而是在项目的根目录中,那么您需要执行以下操作:-

Properties prop = new Properties();          
File configFile = new File(myProp.properties);
InputStream stream = new FileInputStream(configFile);
prop.load(stream);
Run Code Online (Sandbox Code Playgroud)