关于properties文件读取

-----读取-----

load(InputStream inStream)
这个方法可以从.properties属性文件对应的文件输入流中,加载属性列表到Properties类对象
Properties prop = new Properties();
//读取属性文件test.properties(BufferedInputStream 输入流)
InputStream in = new BufferedInputStream (new FileInputStream("test.properties"));
//加载属性列表           
prop.load(in);
//遍历所有key(属性名)
Iterator<String> it=prop.stringPropertyNames().iterator();
while(it.hasNext()){
	String key=it.next();
	//获取value(属性值)
	System.out.println(key+":"+prop.getProperty(key));
}
//关闭流
in.close();

-----写入-----

store(OutputStream out, String comments)
这个方法将Properties类对象的属性列表保存到输出流中
Properties prop = new Properties();
//保存属性到test2.properties文件
FileOutputStream out = new FileOutputStream("test2.properties", true);//true表示追加打开
//设置value(属性值)
prop.setProperty("phone", "10086");
//如果"The New properties file"不为空,保存后的属性文件第一行会是#The New properties file表示注释信息;如果为空则没有注释信息。
prop.store(out, "The New properties file");
//关闭流
out.close();

发布了38 篇原创文章 · 获赞 11 · 访问量 6万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章