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
|
package
cn.zifangsky.serializable;
import
java.io.Serializable;
public
class
WebSite
implements
Serializable {
private
static
final
long
serialVersionUID = 1835573222135484360L;
private
String siteName;
private
String siteUrl;
private
String webMaster;
public
WebSite() {
}
public
WebSite(String siteName, String siteUrl, String webMaster) {
this
.siteName = siteName;
this
.siteUrl = siteUrl;
this
.webMaster = webMaster;
}
public
String getSiteName() {
return
siteName;
}
public
void
setSiteName(String siteName) {
this
.siteName = siteName;
}
public
String getSiteUrl() {
return
siteUrl;
}
public
void
setSiteUrl(String siteUrl) {
this
.siteUrl = siteUrl;
}
public
String getWebMaster() {
return
webMaster;
}
public
void
setWebMaster(String webMaster) {
this
.webMaster = webMaster;
}
public
String toString() {
return
"WebSite [siteName="
+ siteName +
", siteUrl="
+ siteUrl +
", webMaster="
+ webMaster +
"]"
;
}
}
|
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
53
|
package
cn.zifangsky.serializable;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
public
class
TestSerialize {
public
static
void
main(String[] args)
throws
FileNotFoundException, IOException, ClassNotFoundException {
TestSerialize.serializeWebSite();
TestSerialize.deserializeWebSite();
}
/**
* 使用ObjectOutputStream 序列化WebSite
* @throws IOException
* @throws FileNotFoundException
*
* */
public
static
void
serializeWebSite()
throws
FileNotFoundException, IOException{
WebSite webSite =
new
WebSite();
webSite.setSiteName(
"zifangsky的个人博客"
);
webSite.setSiteUrl(
"http://www.zifangsky.cn"
);
webSite.setWebMaster(
"zifangsky"
);
//序列化
ObjectOutputStream objectOutputStream =
new
ObjectOutputStream(
new
FileOutputStream(
new
File(
"C:/Users/Administrator/Desktop/test.txt"
)));
objectOutputStream.writeObject(webSite);
objectOutputStream.flush();
objectOutputStream.close();
}
/**
* 使用ObjectInputStream 反序列化WebSite
* @throws IOException
* @throws FileNotFoundException
* @throws ClassNotFoundException
*
* */
public
static
void
deserializeWebSite()
throws
FileNotFoundException, IOException, ClassNotFoundException{
ObjectInputStream objectInputStream =
new
ObjectInputStream(
new
FileInputStream(
new
File(
"C:/Users/Administrator/Desktop/test.txt"
)));
//反序列化
WebSite webSite = (WebSite) objectInputStream.readObject();
objectInputStream.close();
System.out.println(webSite);
}
}
|
1
|
WebSite [siteName=zifangsky的个人博客, siteUrl=http://www.zifangsky.cn, webMaster=zifangsky]
|
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
|
package
cn.zifangsky.serializable;
import
java.io.Externalizable;
import
java.io.IOException;
import
java.io.ObjectInput;
import
java.io.ObjectOutput;
public
class
ExternalizableDemo
implements
Externalizable{
private
String name;
static
{
System.out.println(
"调用静态代码块"
);
}
public
ExternalizableDemo() {
System.out.println(
"调用无参构造方法"
);
}
public
ExternalizableDemo(String name) {
this
.name = name;
System.out.println(
"调用有参构造方法"
);
}
public
String getName() {
return
name;
}
public
void
setName(String name) {
this
.name = name;
}
public
String toString() {
return
"ExternalizableDemo [name="
+ name +
"]"
;
}
/**
* ObjectOutputStream会调用writeExternal(ObjectOutput out))这个方法进行序列化
* */
public
void
writeExternal(ObjectOutput out)
throws
IOException {
out.writeObject(name);
}
/**
* ObjectInputStream会调用readExternal(ObjectInput in)这个方法进行反序列化
* */
public
void
readExternal(ObjectInput in)
throws
IOException, ClassNotFoundException {
name = (String) in.readObject();
}
}
|
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
|
package
cn.zifangsky.serializable;
import
java.io.File;
import
java.io.FileInputStream;
import
java.io.FileNotFoundException;
import
java.io.FileOutputStream;
import
java.io.IOException;
import
java.io.ObjectInputStream;
import
java.io.ObjectOutputStream;
public
class
TestExternalizable {
public
static
void
main(String[] args)
throws
FileNotFoundException, IOException, ClassNotFoundException {
ExternalizableDemo demo =
new
ExternalizableDemo(
"hello"
);
// 序列化
ObjectOutputStream objectOutputStream =
new
ObjectOutputStream(
new
FileOutputStream(
new
File(
"C:/Users/Administrator/Desktop/test2.txt"
)));
objectOutputStream.writeObject(demo);
objectOutputStream.flush();
objectOutputStream.close();
//反序列化
ObjectInputStream objectInputStream =
new
ObjectInputStream(
new
FileInputStream(
new
File(
"C:/Users/Administrator/Desktop/test2.txt"
)));
ExternalizableDemo demo2 = (ExternalizableDemo) objectInputStream.readObject();
objectInputStream.close();
System.out.println(demo2);
}
}
|
1
2
3
4
|
调用静态代码块
调用有参构造方法
调用无参构造方法
ExternalizableDemo [name=hello]
|