Skip to content

Commit 96bad30

Browse files
committed
关键字transient与序列化
1 parent 4b19e71 commit 96bad30

4 files changed

Lines changed: 282 additions & 0 deletions

File tree

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33

44
在这里,通过文章和代码,把这些知识点和技术的主要内容记录并汇总,供自己快速回顾,也分享给他人。
55

6+
## 关键字
7+
* [transient与序列化](src/cn/aofeng/demo/java/lang/TransientDemo.java)
8+
69
## 数据结构
710
* [在控制台输出类似目录树的结构](src/cn/aofeng/demo/tree)
811

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package cn.aofeng.demo.java.lang.serialization;
2+
3+
import java.io.Externalizable;
4+
import java.io.IOException;
5+
import java.io.ObjectInput;
6+
import java.io.ObjectOutput;
7+
8+
/**
9+
* 自定义序列化和反序列化。
10+
*
11+
* @author <a href="mailto:[email protected]">聂勇</a>
12+
*/
13+
public class Man implements Externalizable {
14+
15+
private String manName;
16+
17+
private int manAge;
18+
19+
private transient String password;
20+
21+
public Man() {
22+
// nothing
23+
}
24+
25+
public Man(String name, int age) {
26+
this.manName = name;
27+
this.manAge = age;
28+
}
29+
30+
public Man(String name, int age, String password) {
31+
this.manName = name;
32+
this.manAge = age;
33+
this.password = password;
34+
}
35+
36+
public String getManName() {
37+
return manName;
38+
}
39+
40+
public void setManName(String manName) {
41+
this.manName = manName;
42+
}
43+
44+
public int getManAge() {
45+
return manAge;
46+
}
47+
48+
public void setManAge(int manAge) {
49+
this.manAge = manAge;
50+
}
51+
52+
public String getPassword() {
53+
return password;
54+
}
55+
56+
public void setPassword(String password) {
57+
this.password = password;
58+
}
59+
60+
@Override
61+
public void writeExternal(ObjectOutput out) throws IOException {
62+
out.writeObject(manName);
63+
out.writeInt(manAge);
64+
out.writeObject(password);
65+
}
66+
67+
@Override
68+
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
69+
manName = (String) in.readObject();
70+
manAge = in.readInt();
71+
password = (String) in.readObject();
72+
}
73+
74+
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package cn.aofeng.demo.java.lang.serialization;
2+
3+
import java.io.Serializable;
4+
5+
/**
6+
* 默认序列化和
7+
*
8+
* @author <a href="mailto:[email protected]">聂勇</a>
9+
*/
10+
public class People implements Serializable {
11+
12+
private static final long serialVersionUID = 6235620243018494633L;
13+
14+
private String name;
15+
16+
private int age;
17+
18+
private transient String address;
19+
20+
private static String sTestNormal;
21+
22+
private static transient String sTestTransient;
23+
24+
public People(String name) {
25+
this.name = name;
26+
}
27+
28+
public People(String name, int age) {
29+
this.name = name;
30+
this.age = age;
31+
}
32+
33+
public People(String name, int age, String address) {
34+
this.name = name;
35+
this.age = age;
36+
this.address = address;
37+
}
38+
39+
public String getName() {
40+
return name;
41+
}
42+
43+
public void setName(String name) {
44+
this.name = name;
45+
}
46+
47+
public int getAge() {
48+
return age;
49+
}
50+
51+
public void setAge(int age) {
52+
this.age = age;
53+
}
54+
55+
public String getAddress() {
56+
return address;
57+
}
58+
59+
public void setAddress(String address) {
60+
this.address = address;
61+
}
62+
63+
public String getsTestNormal() {
64+
return sTestNormal;
65+
}
66+
67+
public void setsTestNormal(String sTestNormal) {
68+
People.sTestNormal = sTestNormal;
69+
}
70+
71+
public String getsTestTransient() {
72+
return sTestTransient;
73+
}
74+
75+
public void setsTestTransient(String sTestTransient) {
76+
People.sTestTransient = sTestTransient;
77+
}
78+
79+
}
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
package cn.aofeng.demo.java.lang.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.ObjectInputStream;
8+
import java.io.ObjectOutputStream;
9+
10+
import org.apache.commons.io.IOUtils;
11+
import org.slf4j.Logger;
12+
import org.slf4j.LoggerFactory;
13+
14+
/**
15+
* 关键字 transient 测试。
16+
*
17+
* @author <a href="mailto:[email protected]">聂勇</a>
18+
*/
19+
public class TransientDemo {
20+
21+
private static Logger _logger = LoggerFactory.getLogger(TransientDemo.class);
22+
23+
private String _tempFileName = "TransientDemo";
24+
25+
/**
26+
* 将对象序列化并保存到文件。
27+
*
28+
* @param obj 待序列化的对象
29+
*/
30+
public void save(Object obj) {
31+
ObjectOutputStream outs = null;
32+
try {
33+
outs = new ObjectOutputStream(
34+
new FileOutputStream( getTempFile(_tempFileName) ));
35+
outs.writeObject(obj);
36+
} catch (IOException e) {
37+
_logger.error("save object to file occurs error", e);
38+
} finally {
39+
IOUtils.closeQuietly(outs);
40+
}
41+
}
42+
43+
/**
44+
* 从文件读取内容并反序列化成对象。
45+
*
46+
* @return {@link People}对象。如果读取文件出错 或 对象类型转换失败,返回null。
47+
*/
48+
public <T> T load() {
49+
ObjectInputStream ins = null;
50+
try {
51+
ins = new ObjectInputStream(
52+
new FileInputStream( getTempFile(_tempFileName)) );
53+
return ((T) ins.readObject());
54+
} catch (IOException e) {
55+
_logger.error("load object from file occurs error", e);
56+
} catch (ClassNotFoundException e) {
57+
_logger.error("load object from file occurs error", e);
58+
} finally {
59+
IOUtils.closeQuietly(ins);
60+
}
61+
62+
return null;
63+
}
64+
65+
private File getTempFile(String filename) {
66+
return new File(getTempDir(), filename);
67+
}
68+
69+
private String getTempDir() {
70+
return System.getProperty("java.io.tmpdir");
71+
}
72+
73+
private void displayPeople(People people) {
74+
if (null == people) {
75+
return;
76+
}
77+
String template = "People[name:%s, age:%d, address:%s, sTestNormal:%s, sTestTransient:%s]";
78+
System.out.println( String.format(template, people.getName(), people.getAge(),
79+
people.getAddress(), people.getsTestNormal(), people.getsTestTransient()));
80+
}
81+
82+
private void displayMan(Man man) {
83+
if (null == man) {
84+
return;
85+
}
86+
String template = "Man[manName:%s, manAge:%d, password:%s]";
87+
System.out.println( String.format(template, man.getManName(), man.getManAge(), man.getPassword()) );
88+
}
89+
90+
/**
91+
* @param args
92+
*/
93+
public static void main(String[] args) {
94+
System.out.println(">>> Serializable测试");
95+
TransientDemo demo = new TransientDemo();
96+
97+
People people = new People("张三", 30, "中国广州");
98+
people.setsTestNormal("normal-first");
99+
people.setsTestTransient("transient-first");
100+
System.out.println("序列化之前的对象信息:");
101+
demo.displayPeople(people);
102+
demo.save(people);
103+
104+
// 修改静态变量的值
105+
people.setsTestNormal("normal-second");
106+
people.setsTestTransient("transient-second");
107+
108+
People fromLoad = demo.load();
109+
System.out.println("反序列化之后的对象信息:");
110+
demo.displayPeople(fromLoad);
111+
112+
113+
114+
System.out.println("");
115+
System.out.println(">>> Externalizable测试");
116+
Man man = new Man("李四", 10, "假密码");
117+
System.out.println("序列化之前的对象信息:");
118+
demo.displayMan(man);
119+
demo.save(man);
120+
121+
Man manFromLoadMan = demo.load();
122+
System.out.println("反序列化之后的对象信息:");
123+
demo.displayMan(manFromLoadMan);
124+
}
125+
126+
}

0 commit comments

Comments
 (0)