1
1
package io ;
2
2
3
- import java .io .*;
3
+ import java .io .BufferedInputStream ;
4
+ import java .io .BufferedOutputStream ;
5
+ import java .io .BufferedReader ;
6
+ import java .io .BufferedWriter ;
7
+ import java .io .EOFException ;
8
+ import java .io .File ;
9
+ import java .io .FileInputStream ;
10
+ import java .io .FileOutputStream ;
11
+ import java .io .FileReader ;
12
+ import java .io .FileWriter ;
13
+ import java .io .IOException ;
14
+ import java .io .InputStream ;
15
+ import java .io .ObjectInputStream ;
16
+ import java .io .ObjectOutputStream ;
17
+ import java .io .OutputStream ;
18
+ import java .io .Serializable ;
4
19
import java .util .ArrayList ;
5
20
import java .util .List ;
6
21
@@ -16,8 +31,7 @@ public class StreamsContinue {
16
31
public void fileInputOutputStream () throws IOException {
17
32
File source = new File ("Zoo.class" );
18
33
File destination = new File ("ZooCopy.class" );
19
- try (InputStream in = new FileInputStream (source );
20
- OutputStream out = new FileOutputStream (destination )) {
34
+ try (InputStream in = new FileInputStream (source ); OutputStream out = new FileOutputStream (destination )) {
21
35
int b ;
22
36
while ((b = in .read ()) != -1 ) {
23
37
out .write (b );
@@ -59,7 +73,7 @@ public void fileReaderWiter() throws IOException {
59
73
}
60
74
61
75
// BufferedReader
62
- public static List <String > readFile (File source ) throws IOException {
76
+ public List <String > readFile (File source ) throws IOException {
63
77
List <String > data = new ArrayList <String >();
64
78
try (BufferedReader reader = new BufferedReader (new FileReader (source ))) {
65
79
String s ;
@@ -71,15 +85,119 @@ public static List<String> readFile(File source) throws IOException {
71
85
}
72
86
73
87
// BufferedWriter
74
- public static void writeFile (List <String > data , File destination ) throws IOException {
88
+ public void writeFile (List <String > data , File destination ) throws IOException {
75
89
try (BufferedWriter writer = new BufferedWriter (new FileWriter (destination ))) {
76
90
for (String s : data ) {
77
91
writer .write (s );
78
92
}
79
93
}
80
94
}
81
95
96
+ // Deserialize
97
+ public List <Animal > getAnimals (File dataFile ) throws IOException , ClassNotFoundException {
98
+ List <Animal > animals = new ArrayList <Animal >();
99
+ try (ObjectInputStream in = new ObjectInputStream (new BufferedInputStream (new FileInputStream (dataFile )))) {
100
+ while (true ) {
101
+ Object object = in .readObject ();
102
+ if (object instanceof Animal )
103
+ animals .add ((Animal ) object );
104
+ }
105
+ } catch (EOFException e ) {
106
+ // File end reached
107
+ }
108
+ return animals ;
109
+ }
110
+
111
+ // Serialize
112
+ public static void createAnimalsFile (List <Animal > animals , File dataFile ) throws IOException {
113
+ try (ObjectOutputStream out = new ObjectOutputStream (
114
+ new BufferedOutputStream (new FileOutputStream (dataFile )))) {
115
+ for (Animal animal : animals )
116
+ out .writeObject (animal );
117
+ }
118
+ }
119
+
82
120
public static void main (String [] args ) throws IOException {
83
121
84
122
}
85
123
}
124
+
125
+ /**
126
+ * Any class can implement the Serializable interface since there are no
127
+ * required methods to implement.
128
+ *
129
+ * Note that the requirement for properly marking an object as Serializable may
130
+ * involve nested objects.
131
+ *
132
+ * Therefore, any object references contained within the Tail class must belong
133
+ * to classes that are also marked as Serializable , and so on.
134
+ *
135
+ * Besides transient instance variables, static class members will also be
136
+ * ignored during the serialization and deserialization process.
137
+ *
138
+ * Why Not Mark Every Class as Serializable ?
139
+ *
140
+ * The reason that we do not is that there are some classes that we want to
141
+ * instruct the JVM not to serialize. In particular, process-heavy classes such
142
+ * as the Thread class or any of the Stream classes would be diffi cult, often
143
+ * impossible, to save to persistent storage, since much of their work involves
144
+ * managing JVM processes or resources in real time.
145
+ *
146
+ */
147
+ class Animal implements Serializable {
148
+ private static final long serialVersionUID = 1L ;
149
+ private String name ;
150
+ private int age ;
151
+ private char type ;
152
+
153
+ public Animal (String name , int age , char type ) {
154
+ this .name = name ;
155
+ this .age = age ;
156
+ this .type = type ;
157
+ }
158
+
159
+ public String getName () {
160
+ return name ;
161
+ }
162
+
163
+ public int getAge () {
164
+ return age ;
165
+ }
166
+
167
+ public char getType () {
168
+ return type ;
169
+ }
170
+
171
+ public String toString () {
172
+ return "Animal [name=" + name + ", age=" + age + ", type=" + type + "]" ;
173
+ }
174
+ }
175
+
176
+ class Animal1 implements Serializable {
177
+ private static final long serialVersionUID = 2L ;
178
+ private transient String name ;
179
+ private transient int age = 10 ;
180
+ private static char type = 'C' ;
181
+ {
182
+ this .age = 14 ;
183
+ }
184
+
185
+ public Animal1 () {
186
+ this .name = "Unknown" ;
187
+ this .age = 12 ;
188
+ this .type = 'Q' ;
189
+ }
190
+
191
+ public Animal1 (String name , int age , char type ) {
192
+ this .name = name ;
193
+ this .age = age ;
194
+ this .type = type ;
195
+ }
196
+ // Same methods as before
197
+
198
+ // [Animal [name=null, age=0, type=P], Animal [name=null, age=0, type=P]]
199
+ /*
200
+ * As expected, you can see that the values for name and age are lost on
201
+ * serialization and not set again during deserialization.
202
+ */
203
+ }
0 commit comments