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

问题描述 投票:18回答:8

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

java eclipse properties
8个回答
10
投票

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


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

enter image description here

/ 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();
        }
    }
}

27
投票

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


4
投票

脚步:

  1. 右键单击要创建.properties文件的任何包,或根据需要创建新包
  2. 现在选择new然后选择文件(如果找不到文件,那么转到包的位置并在那里创建)
  3. 现在将其命名为yourfilename.properties

2
投票

如果您使用'from existing source'选项在eclipse中创建了Java Project,那么它应该像以前一样工作。更准确地说File> New Java Project。在“内容”部分中,选择“从现有源创建项目”,然后选择现有项目文件夹。向导将负责其余的工作。


1
投票
  1. 在eclipse中右键单击项目中要创建属性文件的文件夹
  2. 在搜索过滤器类型文件和连续窗口中的New-> Other->给出扩展名为.properties的文件的名称

1
投票

要创建属性类,请选择要在其中创建属性文件的包。

右键单击包并选择其他。现在选择File并使用(.properties)后缀键入文件名。例如:db.properties。比点击完成。现在,您可以在此属性文件中编写代码。


-2
投票

如果您正在使用核心java,请通过右键单击项目来创建文件(.properties)。如果文件存在于包或src文件夹中,则会抛出找不到文件的错误

© www.soinside.com 2019 - 2024. All rights reserved.