java读写properties配置文件方法
1、Properties类
Properties类表示了一个持久的属性集。Properties可保存在流中或从流中加载,属性列表中的key和value必须是字符串。
虽然Properties类继承了java.util.Hashtable,可以使用Hashtable的put等方法,但是这些方法允许使用非string类型的数据,将导致不安全的行为,所以还是应该使用setProperty 方法。
2、主要方法
load(InputStream in) 从输入流读取属性列表
getProperties(String key) 获取指定key的值,返回string
setProperty(String key, String value) 设置或修改属性的值
store(OutputStream out, String comments) 将properties对象写入一个输出流,comments为注释,comments为空则不加注释
下面进行代码演示
/*初始配置文件
aa=1
bb=2
cc=3
*/ Properties prop = new Properties(); //创建Properties对象
InputStream in = null;
FileOutputStream oFile = null;
try {
in = new FileInputStream("filePath"); //创建输入流文件对象
prop.load(in); //加载输入流
System.out.println("aa=" + prop.getProperty("aa")); //aa=1
prop.setProperty("aa", "11"); //修改"aa"的值
oFile = new FileOutputStream("filePath"); //创建输出流文件对象
prop.store(oFile, ""); //将Properties对象的属性保存到输出流指定文件
} catch (IOException e) {
log.error(e);
} finally {
try {
oFile.close(); //关闭输出流
} catch (IOException e) {
log.error(e);
}
try {
in.close(); //关闭输入流
} catch (IOException e) {
log.error(e);
}
}
最后的关闭IO流很重要,一定要放在finally代码块中执行。
3、修改properties配置文件时遇到的一些问题
读取配置文件一般不会出什么问题,修改和写入的时候稍微复杂一点,把遇到的问题记录一下
3.1 配置FileOutputStream的第二个参数true,导致配置文件末尾重复追加配置项
FileOutputStream构造函数
FileOutputStream(String name, boolean append)
append代表是否向文件末尾追加字符流的形式写入文件,默认为false,即重新写入配置
此处的输出流配置第二个参数为true会导致不停的在配置文件末尾追加重复的配置,导致输出流指定的文件越来越大。所以最好不加这个参数
/*初始配置文件
aa=1
bb=2
cc=3
*/ Properties prop = new Properties();
InputStream in = new FileInputStream("filePath");
prop.load(in);
prop.setProperty("aa", "11");
FileOutputStream oFile = new FileOutputStream("filePath", true);
prop.store(oFile, ""); /*执行后配置文件
aa=1
bb=2
cc=3 aa=11
bb=2
cc=3
*/
3.2 FileOutputStream创建位置导致诡异事情
主要是与setProperty()方法的相对位置
正常是先setProperty()设置属性,然后创建FileOutputStream对象
/*初始配置文件
aa=1
bb=2
cc=3
*/ //正常写法
InputStream in = new FileInputStream("filePath");
prop.load(in);
prop.setProperty("aa", "11");
FileOutputStream oFile = new FileOutputStream("filePath");
prop.store(oFile, ""); //问题写法
InputStream in = new FileInputStream("filePath");
FileOutputStream oFile = new FileOutputStream("filePath"); //提前创建
prop.load(in);
prop.setProperty("aa", "11");
prop.store(oFile, ""); /*正常执行后的配置文件
aa=11
bb=2
cc=3
*/ /*问题执行后的配置文件
aa=11
*/
如果反过来,会导致除setProperty()修改的属性,其它都会丢失。
没想明白这是为什么,有人明白可以指点一下。
3.3 读取和修改properties文件后改变文件内容顺序
使用jdk提供的Properties类读取和修改配置文件后,加载到内存中的顺序是随机的,不能保证和原文件的顺序一致,因此需要重写Properties类,实现顺序读取properties属性。
java读写properties配置文件方法的更多相关文章
- 【转】Java 读写Properties配置文件
[转]Java 读写Properties配置文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形 ...
- Java 读写Properties配置文件
Java 读写Properties配置文件 JAVA操作properties文件 1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了M ...
- (转)Java 读写Properties配置文件
原文:http://www.cnblogs.com/xudong-bupt/p/3758136.html 1.Properties类与Properties配置文件 Properties类继承自Hash ...
- Java 读写Properties配置文件【转】
1.Properties类与Properties配置文件 Properties类继承自Hashtable类并且实现了Map接口,也是使用一种键值对的形式来保存属性集.不过Properties有特殊的地 ...
- java读取properties配置文件方法(一)
为了修改项目参数方便,需要使用properties配置文件: 首先是需要三个jar包(不同的jar包,读取配置文件的方式会有所不同,这里使用的是2.6版本的jar包) commons configur ...
- java读写properties配置文件不改变属性的顺序和注释
先贴代码 import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java ...
- Java 读写Properties配置文件(转)
转自:http://www.cnblogs.com/xudong-bupt/p/3758136.html
- java 顺序 读写 Properties 配置文件
java 顺序 读写 Properties 配置文件 支持中文 不乱码 java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不 ...
- java 顺序 读写 Properties 配置文件 支持中文 不乱码
java 顺序 读写 Properties 配置文件 ,java默认提供的Properties API 继承hashmap ,不是顺序读写的. 特从网上查资料,顺序读写的代码,如下, import j ...
随机推荐
- redis服务部署脚本
yum install -y gcc jemalloc-devel cd /usr/local/src curl -L -O http://download.redis.io/releases/red ...
- mysql count(*) vs count(1)
perfer count(*) 官方文档: _InnoDB handles SELECT COUNT(*) and SELECT COUNT(1) operations in the same way ...
- 正则表达式 ?P<name>
import re # 将匹配的数字乘以 2 def double(matched): value = int(matched.group('value')) return str(value * 2 ...
- python学习之老男孩python全栈第九期_day005作业
1,有如下变量(tu是个元组),请实现要求的功能. tu = ("alex", [11, 22, {"k1": 'v1', "k2": [& ...
- git中的ssh和https方式的使用(gitee为例)
在使用git管理代码,或者使用github,国内的码云(gitee)的时候,有两种方式可以使用,分别是https和ssh,以下均使用gitee为例. ssh方式 配置ssh,如果不配置ssh的话,cl ...
- 【代码笔记】iOS-获得Documents目录
一,代码. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, ...
- 【读书笔记】iOS-网络-错误处理的经验法则
一,在接口契约中处理错误. 二,错误状态可能不正确. 设备模糊地确认操作是崇拜失败的.比如,移动应用发出HTTP请求以在两个账户间转账.请求被银行系统接收并正确地处理:然而,由于网络失败应答却丢失了, ...
- javascript返回上一页的三种写法
window.history.go(-1); //返回上一页 window.history.back(); //返回上一页不会造成死循环 //如果要强行刷新的话就是:window.history.ba ...
- Js利用Canvas实现图片压缩
最近做的APP项目涉及到手机拍照上传图片,因为手机拍照的图片通常都比较大,所以上传的时候就会很慢.为此,需要对图片进行压缩处理来优化上传功能.以下是具体实现: /* * 图片压缩 * img 原始图片 ...
- ebook
libgen.io onlybooks.org www.ebook3000.com www.foxebook.net