Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit55b8d0b

Browse files
serialization is fun
1 parenta13228f commit55b8d0b

File tree

2 files changed

+77
-0
lines changed

2 files changed

+77
-0
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
packagelearnHeadFirstJava.serializationIsFun;
2+
3+
importjava.io.Serializable;
4+
5+
publicclassGospelBookimplementsSerializable {
6+
privatestaticfinallongserialVersionUID =3141633621593356035L;
7+
privateStringname;
8+
privateStringauthor;
9+
privateintorder;
10+
11+
publicGospelBook(Stringname,Stringauthor,intorder) {
12+
super();
13+
this.name =name;
14+
this.author =author;
15+
this.order =order;
16+
}
17+
publicStringgetName(){
18+
returnname;
19+
}
20+
publicintgetOrder(){
21+
returnorder;
22+
}
23+
publicStringgetAuthor(){
24+
returnauthor;
25+
}
26+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
packagelearnHeadFirstJava.serializationIsFun;
2+
3+
importjava.io.FileInputStream;
4+
importjava.io.FileNotFoundException;
5+
importjava.io.FileOutputStream;
6+
importjava.io.IOException;
7+
importjava.io.ObjectInputStream;
8+
importjava.io.ObjectOutputStream;
9+
10+
publicclassGospelSaverTest {
11+
publicstaticvoidmain(String[]args)throwsClassNotFoundException {
12+
GospelBookmatthew =newGospelBook("Matthew","anonymous",1);
13+
GospelBookmark =newGospelBook("Mark","unknown",2);
14+
GospelBookluke =newGospelBook("Luke","not named",3);
15+
GospelBookjohn =newGospelBook("John","anonymous",4);
16+
17+
try {
18+
ObjectOutputStreamos =newObjectOutputStream(newFileOutputStream("Gospel.ser"));
19+
os.writeObject(mark);
20+
os.writeObject(matthew);
21+
os.writeObject(luke);
22+
os.writeObject(john);
23+
os.close();
24+
}catch (FileNotFoundExceptione) {
25+
e.printStackTrace();
26+
}catch (IOExceptione) {
27+
e.printStackTrace();
28+
}
29+
john =null;
30+
matthew =null;
31+
luke =null;
32+
mark =null;
33+
34+
try {
35+
ObjectInputStreamis =newObjectInputStream(newFileInputStream("Gospel.ser"));
36+
GospelBookrestore1 = (GospelBook)is.readObject();
37+
GospelBookrestore2 = (GospelBook)is.readObject();
38+
GospelBookrestore3 = (GospelBook)is.readObject();
39+
GospelBookrestore4 = (GospelBook)is.readObject();
40+
System.out.println("restore1 name is: " +restore1.getName());
41+
System.out.println("restore2 name is: " +restore2.getName());
42+
System.out.println("restore3 name is: " +restore3.getName());
43+
System.out.println("restore4 cnamelass is: " +restore4.getName());
44+
}catch (FileNotFoundExceptione) {
45+
e.printStackTrace();
46+
}catch (IOExceptione) {
47+
e.printStackTrace();
48+
}
49+
System.out.println("Program ended.");
50+
}
51+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp