1.如何创建.properties文件
很简单,建立一个txt文件,并把后缀改成.properties即可
2.将.properties文件拷入src的根目录下
3..properties文件内容格式
#注释
key=value
key2=value1
4.操作代码
/*
* 从配置文件中读取字段
*/
public String GetValueByProperties(String KeyStr) {
String result = "";
try {
InputStream in = this.getClass().getResourceAsStream("/config.properties");
BufferedReader bf = new BufferedReader(new InputStreamReader(in));//据说这一句使用来读取中文用的
Properties p = new Properties();
p.load(bf);
result = p.get(KeyStr).toString().trim();
} catch (Exception ex) {
}
return result;
}
说明:关于.properties读取中文问题
一定要注意一点,.properties文件的编码格式一定项目一致。