一.创建JavaBean
import java.io.Serializable;
public class Student implements Serializable{
/**
*
*/
private static final long serialVersionUID = 3566427234289933929L;
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String toString(){
return id+","+name;
}
}
import java.io.Serializable;
import java.util.List;
public class Teacher implements Serializable {
/**
*
*/
private static final long serialVersionUID = 8284383475131070516L;
private String name;
private List
studentList;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List
getStudentList() {
return studentList;
}
public void setStudentList(List
studentList) {
this.studentList = studentList;
}
}
public class UnserialBean {
private int id;
private String name;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
二.XML序列化和反序列化
import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
public class XMLSerial {
public static void writeObjectToFile(Object obj) throws FileNotFoundException {
File targetFile = new File("xml/serial.xml");
XMLEncoder xe = new XMLEncoder(new FileOutputStream(targetFile), "GBK", true, 0); // 仅用于Java
// SE
// 7
xe.writeObject(obj); // 序列化成XML字符串
xe.close();
}
public static Object readFromFile() throws FileNotFoundException {
File targetFile = new File("xml/serial.xml");
XMLDecoder xd = new XMLDecoder(new FileInputStream(targetFile));
Object obj = xd.readObject(); // 从XML序列中解码为Java对象
xd.close();
return obj;
}
}
三.Object序列化和反序列化
public class ObjectSerial {
public static void writeObjectToFile(Object obj) {
File file = new File("dat/test.dat");
FileOutputStream out;
try {
out = new FileOutputStream(file);
ObjectOutputStream objOut = new ObjectOutputStream(out);
objOut.writeObject(obj);
objOut.flush();
objOut.close();
System.out.println("write object success!");
} catch (IOException e) {
System.out.println("write object failed");
e.printStackTrace();
}
}
public static Object readObjectFromFile() {
Object temp = null;
File file = new File("dat/test.dat");
FileInputStream in;
try {
in = new FileInputStream(file);
ObjectInputStream objIn = new ObjectInputStream(in);
temp = objIn.readObject();
objIn.close();
System.out.println("read object success!");
} catch (IOException e) {
System.out.println("read object failed");
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return temp;
}
}
四.比较两者通过文件读写速率
public static void main(String[] args) throws FileNotFoundException {
List
list = new ArrayList<>();
for (int i = 0; i < 1000; i++) {
Student bean = new Student();
bean.setId(i);
bean.setName("niny");
list.add(bean);
}
Teacher teacher = new Teacher();
teacher.setStudentList(list);
teacher.setName("Mike");
Long time = System.currentTimeMillis();
ObjectSerial.writeObjectToFile(teacher);
System.out.println("success with ObjectSerial " + (System.currentTimeMillis() - time) + " ss");
time = System.currentTimeMillis();
XMLSerial.writeObjectToFile(teacher);
System.out.println("success with write XMLSerial " + (System.currentTimeMillis() - time) + " ss");
time = System.currentTimeMillis();
ObjectSerial.readObjectFromFile();
System.out.println("success with read ObjectSerial " + (System.currentTimeMillis() - time) + " ss");
time = System.currentTimeMillis();
XMLSerial.readFromFile();
System.out.println("success with read XMLSerial " + (System.currentTimeMillis() - time) + " ss");
UnserialBean bean = new UnserialBean();
bean.setId(11);
bean.setName("Liny");
time = System.currentTimeMillis();
XMLSerial.writeObjectToFile(bean);
System.out.println("success with write unserial obj XMLSerial " + (System.currentTimeMillis() - time) + " ss");
time = System.currentTimeMillis();
ObjectSerial.writeObjectToFile(bean);
System.out.println("success with write unserial obj ObjectSerial " + (System.currentTimeMillis() - time) + " ss");
}
五.Console输出结果
success with ObjectSerial 93 ss
success with write XMLSerial 722 ss
read object success!
success with read ObjectSerial 123 ss
success with read XMLSerial 318 ss
success with write unserial obj XMLSerial 4 ss
write object failed
success with write unserial obj ObjectSerial 80 ss
java.io.NotSerializableException: com.hl.serial.bean.UnserialBean
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at com.hl.serial.ObjectSerial.writeObjectToFile(ObjectSerial.java:17)
at com.hl.serial.Main.main(Main.java:49)
success with write XMLSerial 722 ss
read object success!
success with read ObjectSerial 123 ss
success with read XMLSerial 318 ss
success with write unserial obj XMLSerial 4 ss
write object failed
success with write unserial obj ObjectSerial 80 ss
java.io.NotSerializableException: com.hl.serial.bean.UnserialBean
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
at com.hl.serial.ObjectSerial.writeObjectToFile(ObjectSerial.java:17)
at com.hl.serial.Main.main(Main.java:49)
六.总结
A.Object序列方式比较快,存储空间小
B.XML方式不需要Bean序列化,而Object序列化必须在Bean继承于Serializable接口