一、什么是对象序列化?
对象序列化:就是将一个对象转换为二进制数据流。
如果一个类对象要想实现对象序列化,怎对象所在的类必须实现Serializable(序列化)接口。
在此接口中没有任何的方法,此接口只是作为一个标识,表示本类的对象具备了序列化的能力而已。
如果要想完成对象的序列化,则还要依靠ObjectOutputStream类和 ObjectInputStream类,前者属于序列化操作,后者属于反序列化操作。
package com.demo.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class SerializableDemo {
public static void main(String[] args) throws IOException {
OutputStream outputStream= new FileOutputStream("g:"+File.separator+"SerializableDemo.txt");
ObjectOutputStream oop = new ObjectOutputStream(outputStream);
Person person = new Person(11, "张三");
oop.writeObject(person);
outputStream.close();
oop.close();
}
}
class Person implements Serializable{
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public String toString(){
return "姓名:"+name +"年龄:"+age;
}
}
//结果: sr com.demo.io.Person窹{NDp I ageL namet Ljava/lang/String;xp t 寮犱笁
对象被序列化之后,就可以通过ObjectInputStream进行反序列化的操作。
package com.demo.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
public class UNSerializableDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
InputStream inputStream= new FileInputStream("g:"+File.separator+"SerializableDemo.txt");
ObjectInputStream ois = new ObjectInputStream(inputStream);
Object object=ois.readObject();
Person person = (Person)object;
System.out.println(person);
}
}
以上的操作实际上是将整个对象进行序列化的操作,如果现在假设类中的某个属性不希望被序列化的话,则要使用transient(短暂的)关键字声明。
package com.demo.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class SerializableDemo {
public static void main(String[] args) throws IOException {
OutputStream outputStream= new FileOutputStream("g:"+File.separator+"SerializableDemo.txt");
ObjectOutputStream oop = new ObjectOutputStream(outputStream);
Person person = new Person(11, "张三");
oop.writeObject(person);
outputStream.close();
oop.close();
}
}
class Person implements Serializable{
private transient int age;
private transient String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public String toString(){
return "姓名:"+name +"年龄:"+age;
}
}
//结果 sr com.demo.io.Person`?e巌覧 xp
package com.demo.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
public class UNSerializableDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
InputStream inputStream= new FileInputStream("g:"+File.separator+"SerializableDemo.txt");
ObjectInputStream ois = new ObjectInputStream(inputStream);
Object object=ois.readObject();
Person person = (Person)object;
System.out.println(person);
}
}
//结果:姓名:null年龄:0
如何同时对多个对象一起进行序列化操作呢?
Object类可以接收任意的引用数据类型,包括数组。
package com.demo.io;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
public class SerializableDemo {
public static void main(String[] args) throws IOException {
OutputStream outputStream= new FileOutputStream("g:"+File.separator+"SerializableDemo.txt");
ObjectOutputStream oop = new ObjectOutputStream(outputStream);
Person persons[] = {new Person(11, "张三"),new Person(12, "李四"),new Person(13, "王五")};
oop.writeObject(persons);
outputStream.close();
oop.close();
}
}
class Person implements Serializable{
private int age;
private String name;
public Person(int age, String name) {
this.age = age;
this.name = name;
}
public String toString(){
return "姓名:"+name +"年龄:"+age;
}
}
//结果 ur [Lcom.demo.io.Person;粸)oz?; xp sr com.demo.io.Person窹{NDp I ageL namet Ljava/lang/String;xp t 寮犱笁sq ~ t 鏉庡洓sq ~
//t 鐜嬩簲
package com.demo.io;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
public class UNSerializableDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
InputStream inputStream= new FileInputStream("g:"+File.separator+"SerializableDemo.txt");
ObjectInputStream ois = new ObjectInputStream(inputStream);
Object object=ois.readObject();
Person persons[] = (Person[])object;
for(Person person:persons){
System.out.println(person);
}
}
}
//结果:姓名:张三年龄:11
//姓名:李四年龄:12
//姓名:王五年龄:13
如果对一组对象进行序列化操作则必须使用数组概念