-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjavaUBM5.java
57 lines (49 loc) · 1.81 KB
/
javaUBM5.java
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
54
55
56
57
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
public class javaUBM5 {
static String path;
static String name;
public static <T extends Serializable> void serialize() {
try {
File f = new File(path);
FileOutputStream file = new FileOutputStream(f);
ObjectOutputStream out = new ObjectOutputStream(file);
out.writeObject(name);
System.out.println("Object has been successfully serialized... ");
out.close();
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public static <T extends Serializable> T deserialize(T ret) {
try {
File f = new File(path);
FileInputStream file = new FileInputStream(f);
ObjectInputStream in = new ObjectInputStream(file);
T name1 = (T) in.readObject();// Automatically T = String extends Serializable interface
// Also we can write "String name = (String) in.readObject();
System.out.println("name1 = " + name1);
System.out.println("Object has been successfully deserialized... ");
in.close();
file.close();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
return ret;
}
public static void main(String[] args) {
path = "C:\\Users\\bosea\\myfile.ser";
name = "MY Name is Java";
serialize();
String f =deserialize("Finished .....");
System.out.println(f);
}
}