利用properties配置文件,存放一些长时间固定且使用范围较广的参数是比较常使用的。xxxx.properties文件格式为:属性名称=属性值
Properties读取文件内容方法如下:
Properties properties = new Properties();
try {
properties.load(PropertiesDemo.class.getClassLoader().getResourceAsStream("工程根目录下的完整路径地址"));
String attribute = properties.getProperty("attribute");
} catch (IOException e) {
}
Properties写文件的内容方法如下:
try {
FileOutputStream fos = new FileOutputStream("文件所在完整路径");
props.put("属性名", "属性值");
props.store(fos, "");
fos.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
}