java - 如何在 Eclipse 中将 Java 属性文件添加到我的 Java 项目中

标签 java eclipse properties

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

最佳答案

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

enter image description here

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

关于java - 如何在 Eclipse 中将 Java 属性文件添加到我的 Java 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3776687/

相关文章:

java - Eclipse,gradle 依赖项(持久化)缺少库

将多个类实例属性声明为数字的 pythonic 方法?

javascript - Typescript - 对象的条件属性

vb.net - VB.NET的表达主体成员吗?

java - 在 JAX-WS 中引发自定义异常

java - 如何每 8 个输入创建一个新行

java - 在没有 .split() 的情况下对字符串中的所有数字求和

java - 在 Maven 构建中使用 Eclipse Java Compiler (ecj)

eclipse - egit 从上游拉取

java - 如何设置编译器选项,使其拒绝该程序,使其无法静态确保正确性?