序列化以及反序列化:
序列化:就是將類中的對象按照流的方式存儲到文本文件中或者再網絡中傳輸 對象---->流數據 序列化流 (ObjectOutputStream)
反序列化:將文本文件中的流對象或者網絡傳輸中的流對象還原成對象 流數據--->對象 反序列化流(ObjectInputStream)
當我們去創建一個自定義類來實現它
例如:
先創建一個學生類並且去實現Serializeable接口:
package com.westos.Test;
import java.io.Serializable;
public class Student implements Serializable{
/**
* 序列化ID
*/
private static final long serialVersionUID = 4142798162962014997L;
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Student(String name, int age) {
super();
this.name = name;
this.age = age;
}
public Student() {
super();
}
@Override
public String toString() {
return "Student [name=" + name + ", age=" + age + "]";
}
}
在創建一個測試類
package com.westos.Test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
public class Test4 {
public static void main(String[] args) throws FileNotFoundException, IOException, ClassNotFoundException {
//序列化方法
write();
//反序列化方法
Student student=read();
//通過反序列化方法從文件中讀取對象
System.out.println(student.getName()+","+student.getAge());
}
/**
* 創建序列化對象
* @throws IOException
* @throws FileNotFoundException
*/
private static void write() throws FileNotFoundException, IOException {
Student student=new Student("張三",15);
//創建對象輸出流,並將Student對象存儲到文件中去
ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream(new File("E:/Student.txt")));
oos.writeObject(student);
System.out.println("Student對象序列化成功!");
oos.close();
}
/**
* 創建反序列化對象
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*/
private static Student read() throws FileNotFoundException, IOException, ClassNotFoundException {
//創建對象輸入流,並將文件中的數據讀取出來
ObjectInputStream ois=new ObjectInputStream(new FileInputStream(new File("E:/Student.txt")));
Student st=(Student) ois.readObject();
System.out.println("Student對象反序列化對象成功!");
return st;
}
}
Properties:屬性集合類
Properties:表示了一個持久的屬性集(簡稱:屬性集合類) extends Hashtable<K,V> Map集合的
可保存在流中或從流中加載。屬性列表中每個鍵及其對應值都是一個字符串。
public Properties():無參構造
我們先用Map集合的功能區遍歷數組中的元素:
例如:
package com.westos.Properties;
import java.util.Properties;
import java.util.Set;
public class PropertiseDome {
public static void main(String[] args) {
//創建屬性集合類對象,該類是繼承了Hashtable集合,所以可以調用Map集合的功能
Properties pro=new Properties();
//添加元素
pro.put("迪麗熱巴",25 );
pro.put("吳宣儀",25 );
pro.put("孟美岐",23 );
System.out.println(pro);
System.out.println("---------------------");
//獲取集合中的所有的鍵
Set<Object> keySet = pro.keySet();
//遍歷集合元素
for(Object key:keySet) {
//獲取集合中鍵對應的值
Object value = pro.get(key);
System.out.println(key+"="+value);
}
}
}
運行結果:
{孟美岐=23, 吳宣儀=25, 迪麗熱巴=25}
---------------------
孟美岐=23
吳宣儀=25
迪麗熱巴=25
接下來讓我們看看properties類的特有方法:
屬性集合類的特有功能:
public Object setProperty(String key, String value) :給屬性列表中添加鍵和值,並且強制都使用String
public Set<String> stringPropertyNames():遍歷的功能
public String getProperty(String key)用指定的鍵在此屬性列表中搜索屬性
例如:
package com.westos.Properties;
import java.util.Properties;
import java.util.Set;
public class PropertiesDome2 {
public static void main(String[] args) {
//創建屬性集合類對象
Properties pro=new Properties();
//添加元素:public Object setProperty(String key, String value) :給屬性列表中添加鍵和值,並且強制都使用String
pro.setProperty("張三", "21");
pro.setProperty("李四", "23");
pro.setProperty("王五", "25");
System.out.println(pro);
System.out.println("-------------------");
//遍歷元素
Set<String> keyset = pro.stringPropertyNames();
for(String key:keyset) {
String value = pro.getProperty(key);
System.out.println(key+"="+value);
}
}
}
運行結果:
{王五=25, 張三=21, 李四=23}
-------------------
王五=25
張三=21
李四=23
屬性集合類中與流相關的方法:
可保存在流中或從流中加載,只能使用屬性集合類
public void store(Writer writer,String comments):把集合中的數據保存文本文件中(屬性集合)
public void load(Reader reader):將文本文件中的數據加載到屬性集合中
例如:
package com.westos.Properties;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Properties;
public class propertiesDome4 {
public static void main(String[] args) throws IOException {
//創建一個屬性集合類對象
Properties pro=new Properties();
//創建一個字符緩衝輸入流
BufferedReader br=new BufferedReader(new FileReader("oos.txt"));
//將流中的元素添加到屬性集合類中
pro.load(br);
//關閉資源
br.close();
System.out.println(pro);
System.out.println("-------------------");
//創建一個字符緩衝輸出流對象
BufferedWriter bw=new BufferedWriter(new FileWriter("file.txt"));
//使用該方法將集合類中的鍵值對添加到指定文本中去
pro.store(bw, "name'content");
//關閉資源
bw.close();
}
}