综述
配置文件本质上就是一个文本文件,文本文件中的每一行都来放置一个键值对, 通常用=来连接键和值。其实自己通过读取一行,分割字符串的方式来实现读取和写入配置文件并无不可,然而有现成的工具就直接用吧。
读取
读取有很多种方式,这里先介绍一种,通过加载inputstream的方式
//加载配置文件
String filePath = ""; //这里文件分隔符/与\\都可以用,而且文件后缀名是什么没有关系
Properties properties = new Properties();
InputStream input = new BufferedInputStream(new FileInputStream(filePath));
properties.load(input);
//通过get(key)获得vaule
for (String key : properties.stringPropertyNames()) {
System.out.println(key + "=" + properties.get(key));
}
写入
//在上文的加载配置文件基础上
//新增一个键值对或根据key修改vaule都使用下述方法
String key = "";
String vaule= "";
properties.setProperty(key , vaule);
后记
先简单记录,后续完善