1
|
package app;
|
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
|
public class App
|
11
|
{
|
12
|
public static void main(String[] args) throws Exception
|
13
|
{
|
14
|
System.out.println("Starting...");
|
15
|
|
16
|
App thisApp = new App();
|
17
|
thisApp.Serialize(new SampleClasses.Simple(), "serialized/simple.out");
|
18
|
SampleClasses.Simple test1 = thisApp.Deserialize("serialized/simple.out");
|
19
|
}
|
20
|
|
21
|
private <T> T Deserialize(String inputFilePath)
|
22
|
{
|
23
|
try
|
24
|
{
|
25
|
FileInputStream fis = new FileInputStream(inputFilePath);
|
26
|
ObjectInputStream ois = new ObjectInputStream(fis);
|
27
|
T deserialized = (T) ois.readObject();
|
28
|
System.out.println(deserialized.toString());
|
29
|
ois.close();
|
30
|
|
31
|
return deserialized;
|
32
|
}
|
33
|
catch (IOException e)
|
34
|
{
|
35
|
e.printStackTrace();
|
36
|
}
|
37
|
catch (ClassNotFoundException e)
|
38
|
{
|
39
|
e.printStackTrace();
|
40
|
}
|
41
|
return null;
|
42
|
}
|
43
|
|
44
|
/**
|
45
|
* Standard way of serializing objects with java.
|
46
|
*
|
47
|
* Taken from example method, doc
|
48
|
**/
|
49
|
private void Serialize(Object obj, String outputPath)
|
50
|
{
|
51
|
try
|
52
|
{
|
53
|
File toWrite = new File(outputPath);
|
54
|
toWrite.createNewFile();
|
55
|
|
56
|
FileOutputStream fos = new FileOutputStream(toWrite, false);
|
57
|
ObjectOutputStream oos = new ObjectOutputStream(fos);
|
58
|
|
59
|
oos.writeObject(obj);
|
60
|
oos.flush();
|
61
|
oos.close();
|
62
|
System.out.println("serialized.");
|
63
|
}
|
64
|
catch (Exception e)
|
65
|
{
|
66
|
e.printStackTrace();
|
67
|
}
|
68
|
}
|
69
|
}
|