我以前使用 Unix 来编译和编辑我的 Java。因为我在类文件所在的当前工作目录中使用了属性文件。现在我已经切换到 Eclipse IDE。我不知道如何在 Eclipse 中添加相同的属性文件。请帮帮我。
最佳答案
- 如果您的项目没有,请在 Java Resources 文件夹下创建文件夹“resources”。
- 使用以下值创建 config.properties 文件。
/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/