package com.qiang.IO6;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class PropertiesTest {
public static void main(String[] args) {
Properties properties =new Properties();
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try {
//创建输入流对象,用于指向磁盘中的属性文件路径
fileInputStream = new FileInputStream("D:/Ideal wrok space/abc.properties");
//根据一个输入流加载磁盘中某个属性文件所有的k/v结构到当前的properties对象中
properties.load(fileInputStream);
System.out.println(properties.get("name"));
System.out.println(properties.get("age"));
//根据可key获取属性文件中对应的值,如果key不存在返回参数2的数据,没有地址
System.out.println(properties.getProperty("address", "没有地址"));
//向properties对象中添加一个k/v结构,但是没有序列化到本次次磁盘
properties.setProperty("address","南京");
fileOutputStream = new FileOutputStream("D:/Ideal wrok space/abc.properties");
//将当前properties对象序列化到磁盘某个属性文件中去
properties.store(fileOutputStream,"属性文件");
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileInputStream!=null){
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fileOutputStream!= null){
try {
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}