properties类和序列化/反序列化

Hashtable的子类,map的子类,无序,key value值均为string

创建properties集合:

Properties prot=new Properties();

存数据:put

prot.put("aa","bb");

取数据:getProperty

prot.getProperty("aa");

配置文件的读取: .load

fis=new FileInputStream("src/aaa/bbb/ccc/pro.properties"); //数据源

将键值对读取到properties集合对象中

prot.load(fis)

配置文件的写入: .store

fos=new FileOutputStream("src/aaa/bbb/ccc/pro.properties"); //目的地

pro.store(fos,"注释");

序列化与反序列化

序列化:将对象写入文件中,需要实体类实现Serializable才可执行序列化。

1.创建实体类的Person对象p

2.明确目的地:FileOutputStream fos=new FileOutputStream("d:\aa\bb\person.txt")

3.创建序列化流ObjectOutputStream oos=new ObjectOutputStream(fos)

4.讲对象写入文件 p.writeObject(p)

5.释放资源oos.close()

反序列化:从文件中读取对象

1.明确数据源:FileInputStream fis=new FileInputStream("d:\aabperson.txt")

2.创建反序列化流: ObjectInputStream ois=new ObjectInputStream(fis)

3.从文本中读取对象ois.readObject() ,为object类型

4.向下转型并存储Person p=(Person)ois.readObject()

5.释放资源ois.close()