对象序列化
- 指把一个对象编程二进制的数据流的一种方法,通过对象序列化可以方便地实现对象的传输或存储;
1. 序列化接口 Serializable
import java.io.Serializable;
public class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "姓名:" + this.name + ";年龄:" + this.age;
}
}
- 如果要完成对象的输入和输出,还必须依靠对象输出流(ObjectOutputStream)和对象输入流(ObjectInputStream),使用对象输出流输出序列化对象的步骤也可以成为序列化,而使用对象输入流读入对象的过程可以成为反序列化;
2. 实现序列化与反序列化
- 实现序列化就是使用对象输出流输出序列化对象,而反序列化就是使用对象输入流输入对象;
a. 序列化
- 如果要输出一个对象,就必须用到 ObjectOutputStream 类,该类是 OutputStream 的子类,其使用形式和使用 PrintStream 的形式相似,在实例化的过程中也需要传入一个 OutputStream 的子类对象,根据传入的 OutputStream 子类的对象不同,输出的位置也不同;
- java.io.ObjectOutputStream,详见:Class ObjectOutputStream;
- 序列化实例(向文件中写入 Person 对象):
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
File f = new File("C:\\Users\\80626\\Desktop\\1.txt");
ObjectOutputStream oos = null;
OutputStream out = new FileOutputStream(f);
oos = new ObjectOutputStream(out);
oos.writeObject(new Person("张三", 30));
oos.close();
}
}
class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "姓名:" + this.name + ";年龄:" + this.age;
}
}
- 文件中保存的是二进制数据,不能直接对该文件进行修改,否则会破坏其保存格式;
b. 反序列化
- 反序列化则需要使用对象输入流 ObjectInputStream,该类是 InputStream 类的子类,使用方法与 PrintStream 类似,也需要接收 InputStream 类的实例才能实例化;
- 反序列化实例(从文件中读出 Person 对象):
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
File f = new File("C:\\Users\\80626\\Desktop\\1.txt");
ObjectInputStream ois = null;
InputStream input = new FileInputStream(f);
ois = new ObjectInputStream(input);
Object obj = ois.readObject();
ois.close();
System.out.println(obj);
}
}
class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "姓名:" + this.name + ";年龄:" + this.age;
}
}
3. transient 关键字
- Serializable 接口实现的操作实际上是将一个对象中的全部属性序列化,当进行序列化操作时,如果一个对象中的某个属性不希望被序列化,则可以使用 transient 声明,例如:
import java.io.Serializable;
public class Person implements Serializable {
private transient String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "姓名:" + this.name + ";年龄:" + this.age;
}
}
4. 综合运用
import java.io.*;
public class Test {
public static void main(String[] args) throws Exception {
ser();
dser();
}
public static void ser() throws Exception {
File f = new File("C:\\Users\\80626\\Desktop\\1.txt");
ObjectOutputStream oos = null;
OutputStream out = new FileOutputStream(f);
oos = new ObjectOutputStream(out);
oos.writeObject(new Person("张三", 30));
oos.close();
}
public static void dser() throws Exception {
File f = new File("C:\\Users\\80626\\Desktop\\1.txt");
ObjectInputStream ois = null;
InputStream input = new FileInputStream(f);
ois = new ObjectInputStream(input);
Object obj = ois.readObject();
ois.close();
System.out.println(obj);
}
}
class Person implements Serializable {
private transient String name;
private transient int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String toString() {
return "姓名:" + this.name + ";年龄:" + this.age;
}
}