package com.io.homework; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileInputStream; import java.io.FileN ...
package com.io.homework;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.junit.Test;
/**
*序列化和反序列化
*/
public class SerializeDemo{
/*
* 序列化 : 創建三個不同的Person對象,將這三個對象序列化到文件中;
*/
@Test
public void serializeTest() {
Person person1 = new Person("李明", 24, 60.2);
Person person2 = new Person("jack", 22, 67.5);
Person person3 = new Person("斯密瑟", 26, 70.2);
List<Person> list = new ArrayList<>();
list.add(person1);
list.add(person2);
list.add(person3);
// 3、創建ObjectOutputStream 的實例完成序列化
ObjectOutputStream oos = null;
try {
// 1、創建輸出流 FileOutputStream
FileOutputStream fos = new FileOutputStream("./serialize.dat");
// 2、創建緩衝包裝節點流
BufferedOutputStream bos = new BufferedOutputStream(fos);
oos = new ObjectOutputStream(bos);
// 4、通過標識對應的數據類型,將數據寫入目標地點
oos.writeObject(list);
} catch (IOException e) {
e.printStackTrace();
} finally {
// 5、關閉流
if (oos != null) {
try {
oos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
System.out.println("寫入成功!!");
}
/*
* 反序列化:三個Person對象,列印輸出。驗證序列化的正確;
*/
@Test
public void deserializationTest() {
// 2. 創建 ObjectInputStream 的實例,包裝現有節點流,用於完成反序列化
ObjectInputStream ois = null;
try {
// 1. 創建 FileInputStream 的實例
FileInputStream fis = new FileInputStream("./serialize.dat");
BufferedInputStream bis = new BufferedInputStream(fis);
ois = new ObjectInputStream(bis);
@SuppressWarnings("unchecked")
List<Person> list = (List<Person>) ois.readObject();
Iterator<Person> it = list.iterator();
// 3. 讀取文件的內容
while (it.hasNext()) {
Person person = it.next();
System.out.println(person);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ois != null) {
try {
ois.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
//編寫Person類,包含姓名、年齡、體重等屬性,提供對應的訪問方法;
class Person implements Serializable {
private static final long serialVersionUID = 314398401529574184L;
private String name;// 姓名
private Integer age;// 年齡
private double whight;// 體重
/**
* @param name
* @param age
* @param whight
*/
public Person(String name, Integer age, double whight) {
super();
this.name = name;
this.age = age;
this.whight = whight;
}
public Person() {
super();
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the age
*/
public Integer getAge() {
return age;
}
/**
* @param age the age to set
*/
public void setAge(Integer age) {
this.age = age;
}
/**
* @return the whight
*/
public double getWhight() {
return whight;
}
/**
* @param whight the whight to set
*/
public void setWhight(double whight) {
this.whight = whight;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + ", whight=" + whight + "]";
}
}