Skip to content

Commit 2d4b6bd

Browse files
committed
🔖 序列化示例
1 parent 2909982 commit 2d4b6bd

8 files changed

Lines changed: 691 additions & 0 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package io.github.dunwu.javase.serialization;
2+
3+
import java.io.Externalizable;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.ObjectInput;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutput;
12+
import java.io.ObjectOutputStream;
13+
import java.io.OutputStream;
14+
15+
/**
16+
* 序列化示例
17+
* @author Zhang Peng
18+
* @date 2018/6/4
19+
* @see SerializeDemo01
20+
* @see ExternalizeDemo01
21+
* @see UnSerializeDemo
22+
*/
23+
@SuppressWarnings("all")
24+
public class ExternalizeDemo01 {
25+
enum Sex {
26+
MALE, FEMALE
27+
}
28+
29+
static class Person implements Externalizable {
30+
private static final long serialVersionUID = 1L;
31+
private String name = null;
32+
transient private Integer age = null;
33+
private Sex sex;
34+
35+
public Person() {
36+
System.out.println("call Person()");
37+
}
38+
39+
public Person(String name, Integer age, Sex sex) {
40+
this.name = name;
41+
this.age = age;
42+
this.sex = sex;
43+
}
44+
45+
private void writeObject(ObjectOutputStream out) throws IOException {
46+
out.defaultWriteObject();
47+
out.writeInt(age);
48+
}
49+
50+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
51+
in.defaultReadObject();
52+
age = in.readInt();
53+
}
54+
55+
@Override
56+
public void writeExternal(ObjectOutput out) throws IOException { }
57+
58+
@Override
59+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException { }
60+
61+
public String toString() {
62+
return "name: " + this.name + ", age: " + this.age + ", sex: " + this.sex;
63+
}
64+
}
65+
66+
/**
67+
* 序列化
68+
*/
69+
private static void serialize(String filename) throws IOException {
70+
File f = new File(filename); // 定义保存路径
71+
OutputStream out = new FileOutputStream(f); // 文件输出流
72+
ObjectOutputStream oos = new ObjectOutputStream(out); // 对象输出流
73+
oos.writeObject(new Person("Jack", 30, Sex.MALE)); // 保存对象
74+
oos.close();
75+
out.close();
76+
}
77+
78+
/**
79+
* 反序列化
80+
*/
81+
private static void deserialize(String filename) throws IOException, ClassNotFoundException {
82+
File f = new File(filename); // 定义保存路径
83+
InputStream in = new FileInputStream(f); // 文件输入流
84+
ObjectInputStream ois = new ObjectInputStream(in); // 对象输入流
85+
Object obj = ois.readObject(); // 读取对象
86+
ois.close();
87+
in.close();
88+
System.out.println(obj);
89+
}
90+
91+
public static void main(String[] args) throws IOException, ClassNotFoundException {
92+
final String filename = "d:/text.dat";
93+
serialize(filename);
94+
deserialize(filename);
95+
}
96+
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
package io.github.dunwu.javase.serialization;
2+
3+
import java.io.Externalizable;
4+
import java.io.File;
5+
import java.io.FileInputStream;
6+
import java.io.FileOutputStream;
7+
import java.io.IOException;
8+
import java.io.InputStream;
9+
import java.io.ObjectInput;
10+
import java.io.ObjectInputStream;
11+
import java.io.ObjectOutput;
12+
import java.io.ObjectOutputStream;
13+
import java.io.OutputStream;
14+
15+
/**
16+
* 序列化示例
17+
* @author Zhang Peng
18+
* @date 2018/6/4
19+
* @see SerializeDemo01
20+
* @see ExternalizeDemo02
21+
* @see UnSerializeDemo
22+
*/
23+
@SuppressWarnings("all")
24+
public class ExternalizeDemo02 {
25+
enum Sex {
26+
MALE, FEMALE
27+
}
28+
29+
static class Person implements Externalizable {
30+
private static final long serialVersionUID = 1L;
31+
private String name = null;
32+
transient private Integer age = null;
33+
private Sex sex;
34+
35+
public Person() {
36+
System.out.println("call Person()");
37+
}
38+
39+
public Person(String name, Integer age, Sex sex) {
40+
this.name = name;
41+
this.age = age;
42+
this.sex = sex;
43+
}
44+
45+
private void writeObject(ObjectOutputStream out) throws IOException {
46+
out.defaultWriteObject();
47+
out.writeInt(age);
48+
}
49+
50+
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
51+
in.defaultReadObject();
52+
age = in.readInt();
53+
}
54+
55+
@Override
56+
public void writeExternal(ObjectOutput out) throws IOException {
57+
out.writeObject(name);
58+
out.writeInt(age);
59+
}
60+
61+
@Override
62+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
63+
name = (String) in.readObject();
64+
age = in.readInt();
65+
}
66+
67+
public String toString() {
68+
return "name: " + this.name + ", age: " + this.age + ", sex: " + this.sex;
69+
}
70+
}
71+
72+
/**
73+
* 序列化
74+
*/
75+
private static void serialize(String filename) throws IOException {
76+
File f = new File(filename); // 定义保存路径
77+
OutputStream out = new FileOutputStream(f); // 文件输出流
78+
ObjectOutputStream oos = new ObjectOutputStream(out); // 对象输出流
79+
oos.writeObject(new Person("Jack", 30, Sex.MALE)); // 保存对象
80+
oos.close();
81+
out.close();
82+
}
83+
84+
/**
85+
* 反序列化
86+
*/
87+
private static void deserialize(String filename) throws IOException, ClassNotFoundException {
88+
File f = new File(filename); // 定义保存路径
89+
InputStream in = new FileInputStream(f); // 文件输入流
90+
ObjectInputStream ois = new ObjectInputStream(in); // 对象输入流
91+
Object obj = ois.readObject(); // 读取对象
92+
ois.close();
93+
in.close();
94+
System.out.println(obj);
95+
}
96+
97+
public static void main(String[] args) throws IOException, ClassNotFoundException {
98+
final String filename = "d:/text.dat";
99+
serialize(filename);
100+
deserialize(filename);
101+
}
102+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package io.github.dunwu.javase.serialization;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.ObjectInputStream;
9+
import java.io.ObjectOutputStream;
10+
import java.io.OutputStream;
11+
import java.io.Serializable;
12+
13+
/**
14+
* 序列化示例
15+
* @author Zhang Peng
16+
* @date 2018/6/4
17+
*/
18+
@SuppressWarnings("all")
19+
public class SerializeDemo01 {
20+
enum Sex {
21+
MALE, FEMALE
22+
}
23+
24+
static class Person implements Serializable {
25+
private static final long serialVersionUID = 1L;
26+
private String name = null;
27+
private Integer age = null;
28+
private Sex sex;
29+
30+
public Person() {
31+
System.out.println("call Person()");
32+
}
33+
34+
public Person(String name, Integer age, Sex sex) {
35+
this.name = name;
36+
this.age = age;
37+
this.sex = sex;
38+
}
39+
40+
public String toString() {
41+
return "name: " + this.name + ", age: " + this.age + ", sex: " + this.sex;
42+
}
43+
}
44+
45+
/**
46+
* 序列化
47+
*/
48+
private static void serialize(String filename) throws IOException {
49+
File f = new File(filename); // 定义保存路径
50+
OutputStream out = new FileOutputStream(f); // 文件输出流
51+
ObjectOutputStream oos = new ObjectOutputStream(out); // 对象输出流
52+
oos.writeObject(new Person("Jack", 30, Sex.MALE)); // 保存对象
53+
oos.close();
54+
out.close();
55+
}
56+
57+
/**
58+
* 反序列化
59+
*/
60+
private static void deserialize(String filename) throws IOException, ClassNotFoundException {
61+
File f = new File(filename); // 定义保存路径
62+
InputStream in = new FileInputStream(f); // 文件输入流
63+
ObjectInputStream ois = new ObjectInputStream(in); // 对象输入流
64+
Object obj = ois.readObject(); // 读取对象
65+
ois.close();
66+
in.close();
67+
System.out.println(obj);
68+
}
69+
70+
public static void main(String[] args) throws IOException, ClassNotFoundException {
71+
final String filename = "d:/text.dat";
72+
serialize(filename);
73+
deserialize(filename);
74+
}
75+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package io.github.dunwu.javase.serialization;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileOutputStream;
6+
import java.io.IOException;
7+
import java.io.InputStream;
8+
import java.io.ObjectInputStream;
9+
import java.io.ObjectOutputStream;
10+
import java.io.OutputStream;
11+
import java.io.Serializable;
12+
13+
/**
14+
* 序列化示例
15+
* @author Zhang Peng
16+
* @date 2018/6/4
17+
* @see SerializeDemo01
18+
* @see SerializeDemo02
19+
* @see UnSerializeDemo
20+
*/
21+
@SuppressWarnings("all")
22+
public class SerializeDemo02 {
23+
24+
enum Sex {
25+
MALE, FEMALE
26+
}
27+
28+
static class Person implements Serializable {
29+
private static final long serialVersionUID = 1L;
30+
private String name = null;
31+
transient private Integer age = null;
32+
private Sex sex;
33+
34+
public Person() {
35+
System.out.println("call Person()");
36+
}
37+
38+
public Person(String name, Integer age, Sex sex) {
39+
this.name = name;
40+
this.age = age;
41+
this.sex = sex;
42+
}
43+
44+
public String toString() {
45+
return "name: " + this.name + ", age: " + this.age + ", sex: " + this.sex;
46+
}
47+
}
48+
49+
/**
50+
* 序列化
51+
*/
52+
private static void serialize(String filename) throws IOException {
53+
File f = new File(filename); // 定义保存路径
54+
OutputStream out = new FileOutputStream(f); // 文件输出流
55+
ObjectOutputStream oos = new ObjectOutputStream(out); // 对象输出流
56+
oos.writeObject(new Person("Jack", 30, Sex.MALE)); // 保存对象
57+
oos.close();
58+
out.close();
59+
}
60+
61+
/**
62+
* 反序列化
63+
*/
64+
private static void deserialize(String filename) throws IOException, ClassNotFoundException {
65+
File f = new File(filename); // 定义保存路径
66+
InputStream in = new FileInputStream(f); // 文件输入流
67+
ObjectInputStream ois = new ObjectInputStream(in); // 对象输入流
68+
Object obj = ois.readObject(); // 读取对象
69+
ois.close();
70+
in.close();
71+
System.out.println(obj);
72+
}
73+
74+
public static void main(String[] args) throws IOException, ClassNotFoundException {
75+
final String filename = "d:/text.dat";
76+
serialize(filename);
77+
deserialize(filename);
78+
}
79+
}

0 commit comments

Comments
 (0)