我想创建一个config.properties文件,我想在其中存储所有的键和值,而不是在Java代码中对它们进行硬编码.
但是,我不知道如何在eclipse中创建属性文件.我研究并找到了有关如何读取属性文件的帮助.我需要有关如何创建它的帮助.
以下是我的具体问题:
我将非常感谢任何帮助.
mir*_*sif 15
然后,您可以像这样添加属性文件的属性
dbpassword=password
database=localhost
dbuser=user
Run Code Online (Sandbox Code Playgroud)
加载属性的示例
public class App {
public static void main(String[] args) {
Properties prop = new Properties();
InputStream input = null;
try {
input = new FileInputStream("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)


| 归档时间: |
|
| 查看次数: |
49112 次 |
| 最近记录: |