好记性不如赖笔头…………
Properties集合:
- 继承于HashTable,是线程安全的
- Properties可以保存到流或流中加载
- 属性列表中的每个键和它的相应值是一个字符串
- 没有泛型
- 不建议使用put/putAll方法,建议使用setProperty/getProperty
package com.Ckinghan.properties;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.Set;
/**
* @author Ckinghan
* @描述:集合Properties
* @特点:
* 1、继承于HashTable,是线程安全的
* 2、Properties可以保存到流或流中加载
* 3、属性列表中的每个键和它的相应值是一个字符串
* 4、没有泛型
* 5、不建议使用put/putAll方法,建议使用setProperty/getProperty
*/
public class PropertiesDemo {
public static void main(String[] args) {
/**
* 创建Properties集合,并写入到文件中
*/
createProperties();
/**
* 使用Properties自带的方法将集合中的数据写入到文件 中
*/
createPropertiesSaveFile();
/**
* 使用Properties自带的方法store将集合中的数据写入到文件 中,建议使用
*/
createPropertiesStoreSaveFile();
/**
* 读取文件中的数据到Properties集合中
*/
readerPropertiesData();
}
/**
* @描述:读取文件中的数据到Properties集合中
* @创建时间:
*/
public static void readerPropertiesData(){
Properties properties = new Properties();
BufferedReader bufferedReader = null;
try {
bufferedReader = new BufferedReader(new FileReader("PropertiesStore.txt"));
//读取 文件中的数据到propertis中
properties.load(bufferedReader);
//输出
System.out.println(properties);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally {
if(bufferedReader != null){
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
/**
* @描述:使用Properties自带的方法store将集合中的数据写入到文件 中,建议使用
* @创建时间:
*/
public static void createPropertiesStoreSaveFile(){
//创建Propertis集合并写入到文件中
Properties properties = new Properties();
properties.setProperty("name", "小明");
properties.setProperty("age", "30");
properties.setProperty("sex", "男人");
properties.setProperty("class", "一班");
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter(new FileWriter("PropertiesStore.txt"),true);
/**
* 使用Properties集合提供 的方法将数据写入到文件 中
*/
properties.store(printWriter,null);
System.out.println("通过Properties集合提供的方法store写入到文件的操作成功");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(printWriter != null){
printWriter.close();
}
}
}
/**
* @描述:使用Properties自带的方法list将集合中的数据写入到文件 中
* @创建时间:
*/
public static void createPropertiesSaveFile(){
//创建Propertis集合并写入到文件中
Properties properties = new Properties();
properties.setProperty("name", "小明");
properties.setProperty("age", "30");
properties.setProperty("sex", "男人");
properties.setProperty("class", "一班");
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter(new FileWriter("Properties.txt"),true);
/**
* 使用Properties集合提供 的方法将数据写入到文件 中
*/
properties.list(printWriter);
System.out.println("通过Properties集合提供的方法写入到文件的操作成功");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(printWriter != null){
printWriter.close();
}
}
}
/**
* @描述:创建Properties集合,并写入到文件中
* @创建时间:
*/
public static void createProperties(){
//创建Propertis集合并写入到文件中
Properties properties = new Properties();
properties.setProperty("name", "小明");
properties.setProperty("age", "30");
properties.setProperty("sex", "男人");
properties.setProperty("class", "一班");
PrintWriter printWriter = null;
try {
printWriter = new PrintWriter(new FileWriter("Properties.txt"),true);
//将properties集合的数据写入到文件中
Set<Entry<Object, Object>> keys =properties.entrySet();
for(Entry<Object, Object> entry : keys){
printWriter.println(entry.getKey().toString()+"="+entry.getValue().toString());
}
System.out.println("写入文件成功!");
} catch (IOException e) {
e.printStackTrace();
}finally {
if(printWriter != null){
printWriter.close();
}
}
// System.out.println(properties);
}
}
-----------------------------------------------------------------执行结果-----------------------------------------------------------------------
写入文件成功!
通过Properties集合提供的方法写入到文件的操作成功
通过Properties集合提供的方法store写入到文件的操作成功
{age=30, name=小明, sex=男人, class=一班}