Repository navigation
Expand file tree
/
Copy pathTest.java
More file actions
69 lines (55 loc) · 1.59 KB
/
Copy pathTest.java
File metadata and controls
69 lines (55 loc) · 1.59 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
import java.beans.XMLDecoder;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.Serializable;
import java.util.List;
public class Test {
private static final File XML_FILE = new File("12 - File Handling + Serializable/12.4 - Serialization using XMLEncoder/src/obj.txt");
public static void main(String[] args) throws FileNotFoundException {
try {
XMLDecoder x = new XMLDecoder(new BufferedInputStream(new FileInputStream(XML_FILE)));
College c = (College)x.readObject();
List<Student> s = c.getStudents();
for (Student value : s) {
System.out.println(value);
}
} catch (FileNotFoundException e) {
System.out.println(e);
}
}
}
class College implements Serializable {
College() {
}
public List<Student> students;
public List<Student> getStudents() {
return students;
}
public void setStudents(List<Student> students) {
this.students = students;
}
}
class Student implements Serializable {
Student() {
}
private int rollNo;
private String sname;
public int getRollNo() {
return rollNo;
}
public void setRollNo(int rollNo) {
this.rollNo = rollNo;
}
public String getSname() {
return sname;
}
public void setSname(String sname) {
this.sname = sname;
}
@Override
public String toString() {
return "Student{" + "rollNo=" + rollNo + ", sname=" + sname + '}';
}
}