Repository navigation
Expand file tree
/
Copy pathStreams.java
More file actions
84 lines (67 loc) · 2.14 KB
/
Copy pathStreams.java
File metadata and controls
84 lines (67 loc) · 2.14 KB
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
class Stud implements Serializable {
private int code;
private String name;
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Stud(int code, String name) {
super();
this.code = code;
this.name = name;
}
public String toString() {
return "Stud [code=" + code + ", name=" + name + "]";
}
}
public class Streams {
public static void main(String[] args) {
ArrayList<Stud> list = new ArrayList();
list.add(new Stud(1, "Nikhil"));
list.add(new Stud(2, "Neeraj"));
list.add(new Stud(3, "Mansi"));
list.add(new Stud(4, "Mayank"));
for (Stud s : list)
System.out.println(s);
// --------------------Serialization---------//
File file = new File("Stud.ser");
try {
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(list);
oos.close();
System.out.println("Sirialization done...");
// ----------------Desirialization---------//
FileInputStream fis = new FileInputStream(file);
ObjectInputStream ois = new ObjectInputStream(fis);
ArrayList<Stud> tmp = (ArrayList<Stud>) ois.readObject();
for (Stud t : tmp)
System.out.println(t);
ois.close(); // ois streams
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}