1
|
package serialize;
|
2
|
|
3
|
import java.io.File;
|
4
|
import java.io.FileOutputStream;
|
5
|
import java.io.ObjectOutputStream;
|
6
|
import java.io.Serializable;
|
7
|
|
8
|
public class Example extends Parent implements Serializable {
|
9
|
|
10
|
private static final long serialVersionUID = 1L;
|
11
|
public static String SERIALIZED_FILE = "data.out";
|
12
|
|
13
|
private int numberX = 7;
|
14
|
Contain variable = new Contain();
|
15
|
float x = 10;
|
16
|
|
17
|
public long getNumber() {
|
18
|
return numberX;
|
19
|
}
|
20
|
|
21
|
public static boolean serialize(File file) {
|
22
|
boolean retValue = false;
|
23
|
try {
|
24
|
FileOutputStream fos = new FileOutputStream(file);
|
25
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
26
|
Example s = new Example();
|
27
|
oos.writeObject(s);
|
28
|
oos.flush();
|
29
|
oos.close();
|
30
|
System.out.println("File " + SERIALIZED_FILE + " created.");
|
31
|
|
32
|
/*
|
33
|
--- DESERIALIZATION ---
|
34
|
FileInputStream fis = new FileInputStream(Constants.FILE_NAME);
|
35
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
36
|
s = (Example) ois.readObject();
|
37
|
System.out.println("DATA: " + s.string + " " + s.number + " " + Arrays.toString(s.variable.array));
|
38
|
ois.close();
|
39
|
*/
|
40
|
|
41
|
retValue = true;
|
42
|
} catch (Exception e) {
|
43
|
e.printStackTrace();
|
44
|
}
|
45
|
return retValue;
|
46
|
}
|
47
|
|
48
|
public static void main(String[] args) {
|
49
|
serialize(new File(SERIALIZED_FILE));
|
50
|
}
|
51
|
|
52
|
}
|