-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSerializableDemo.java
More file actions
52 lines (43 loc) · 1.46 KB
/
SerializableDemo.java
File metadata and controls
52 lines (43 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
package code_03_object;
import java.io.*;
/**
* 序列化就是将一个对象转换成字节序列,方便存储和传输。
- 序列化:ObjectOutputStream.writeObject()
- 反序列化:ObjectInputStream.readObject()
*/
public class SerializableDemo {
public static void main(String[] args) throws IOException, ClassNotFoundException {
String objectFile="demo6.txt";
//序列化
serialize(objectFile);
//反序列化
deserialize(objectFile);
}
//序列化
public static void serialize(String objectFile) throws IOException {
A a=new A(1,"aaa");
ObjectOutputStream objectOutputStream=new ObjectOutputStream(new FileOutputStream(objectFile));
//序列化
objectOutputStream.writeObject(a);
objectOutputStream.close();
}
//反序列化
public static void deserialize(String objectFile) throws IOException, ClassNotFoundException {
ObjectInputStream objectInputStream=new ObjectInputStream(new FileInputStream(objectFile));
A a2=(A)objectInputStream.readObject();
System.out.println(a2);
objectInputStream.close();
}
private static class A implements Serializable {
private int x;
private transient String y;
A(int x, String y) {
this.x = x;
this.y = y;
}
@Override
public String toString() {
return "x = " + x + " " + "y = " + y;
}
}
}