如何在Eclipse中的Java项目中添加Java Properties文件

Vij*_*yan 18 java eclipse properties

我之前使用Unix来编译和编辑我的Java.我已经在我当前工作目录中使用了属性文件,其中存在类文件.现在我已切换到Eclipse IDE.我不知道如何在Eclipse中添加相同的属性文件.请帮我.

Kha*_*bib 28

  1. 如果您的项目没有,请在Java Resources文件夹下创建文件夹"resources".
    1. 使用以下值创建config.properties文件.

在此输入图像描述

/ Java Resources/resources/config.properties

用于加载属性.

Properties prop = new Properties();
InputStream input = null;

try {

      input = getClass().getClassLoader().getResourceAsStream("config.properties");


    // load a properties file
    prop.load(input);

    // 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();
} finally {
    if (input != null) {
        try {
            input.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Mic*_*rdt 27

在包资源管理器中,右键单击该包并选择New - > File,然后输入包含".properties"后缀的文件名.


Joh*_*olu 10

如果在当前工作目录中有属性文件,它应该在Unix中正常工作.另一种选择是将属性文件添加到类路径并在此处使用this.getClass().getClassLoader().getResourceAsStream("xxxxx.properties"); More 获取输入流