首先,谈一下串行化的好处:
(1)在网络中传送对象。
(2)在程序的运行期间将对象保存于文件,或者稍后在相同的应用程序中写入然后读取对象。
实现对象串行化有两个前提:
(1)要串行化的对象对应的类必须实现Serializable接口。
(2)要串行化的对象对应的类必须是公共的(public)。
在Java中,有两个流类支持对象串行化:ObjectOutputStream和ObjectInputStream。
在对象串行化过程中,如果类中有某些域不想被串行化,则用transient修饰这些域。
下面是一个例子:
Customer.java
1import java.io.*;
2
3public class Customer implements Serializable {
4 private String name, ID;
5 transient private String password;
6 private float balance;
7
8 public Customer(String name, String ID, String password, float balance) {
9 this.name = name;
10 this.ID = ID;
11 this.password = password;
12 this.balance = balance;
13 }
14
15 public String getName() {
16 return name;
17 }
18
19 public String getID() {
20 return ID;
21 }
22
23 public String getPassword() {
24 return password;
25 }
26
27 public float getBalance() {
28 return balance;
29 }
30}
31
32
2
3public class Customer implements Serializable {
4 private String name, ID;
5 transient private String password;
6 private float balance;
7
8 public Customer(String name, String ID, String password, float balance) {
9 this.name = name;
10 this.ID = ID;
11 this.password = password;
12 this.balance = balance;
13 }
14
15 public String getName() {
16 return name;
17 }
18
19 public String getID() {
20 return ID;
21 }
22
23 public String getPassword() {
24 return password;
25 }
26
27 public float getBalance() {
28 return balance;
29 }
30}
31
32
ObjectIODemo.java
1import java.io.*;
2
3public class ObjectIODemo {
4 public static void main(String[] args) {
5 try {
6 ObjectOutputStream objectOut = new ObjectOutputStream
7 (new BufferedOutputStream(new FileOutputStream("object.bin")));
8
9 Customer cust = new Customer("张三", "00001", "1234", 30000);
10 objectOut.writeObject(cust);
11 cust = new Customer("李四", "00002", "5678", 10000);
12 objectOut.writeObject(cust);
13 objectOut.close();
14
15 ObjectInputStream objectIn = new ObjectInputStream
16 (new BufferedInputStream(new FileInputStream("object.bin")));
17
18 cust = (Customer)objectIn.readObject();
19 display(cust);
20
21 cust = (Customer)objectIn.readObject();
22 display(cust);
23
24 objectIn.close();
25 }
26 catch(NotSerializableException e) {
27 System.out.println(e.getMessage());
28 }
29 catch(ClassNotFoundException e) {
30 System.out.println(e.getMessage());
31 }
32 catch(IOException e) {
33 System.out.println(e.getMessage());
34 }
35 }
36
37 private static void display(Customer cust) {
38 System.out.println("Name: " + cust.getName());
39 System.out.println("ID: " + cust.getID());
40 System.out.println("Password: " + cust.getPassword());
41 System.out.println("Balance: " + cust.getBalance());
42 }
43}
44
2
3public class ObjectIODemo {
4 public static void main(String[] args) {
5 try {
6 ObjectOutputStream objectOut = new ObjectOutputStream
7 (new BufferedOutputStream(new FileOutputStream("object.bin")));
8
9 Customer cust = new Customer("张三", "00001", "1234", 30000);
10 objectOut.writeObject(cust);
11 cust = new Customer("李四", "00002", "5678", 10000);
12 objectOut.writeObject(cust);
13 objectOut.close();
14
15 ObjectInputStream objectIn = new ObjectInputStream
16 (new BufferedInputStream(new FileInputStream("object.bin")));
17
18 cust = (Customer)objectIn.readObject();
19 display(cust);
20
21 cust = (Customer)objectIn.readObject();
22 display(cust);
23
24 objectIn.close();
25 }
26 catch(NotSerializableException e) {
27 System.out.println(e.getMessage());
28 }
29 catch(ClassNotFoundException e) {
30 System.out.println(e.getMessage());
31 }
32 catch(IOException e) {
33 System.out.println(e.getMessage());
34 }
35 }
36
37 private static void display(Customer cust) {
38 System.out.println("Name: " + cust.getName());
39 System.out.println("ID: " + cust.getID());
40 System.out.println("Password: " + cust.getPassword());
41 System.out.println("Balance: " + cust.getBalance());
42 }
43}
44
运行结果:
生成了object.bin 文件
输出:
Name: 张三
ID: 00001
Password: null
Balance: 30000.0
Name: 李四
ID: 00002
Password: null
Balance: 10000.0