-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStudentManagement.java
More file actions
54 lines (39 loc) · 1.14 KB
/
StudentManagement.java
File metadata and controls
54 lines (39 loc) · 1.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
/**
*
*/
package filesIO;
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.util.Date;
import java.util.HashMap;
/**
* @author rutpatel
*
*/
public class StudentManagement {
public static void main(String[] args) throws IOException, ClassNotFoundException {
HashMap<Integer, Student> std = new HashMap<Integer, Student>();
for (int i = 1; i < 5; i++) {
Student s = new Student(i, "Name - " + i, new Date());
std.put(i, s);
}
File f = new File("student.txt");
FileOutputStream fos = new FileOutputStream(f);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(std);
oos.flush();
oos.close();
System.out.println("Student Data Added...");
FileInputStream fis = new FileInputStream(f);
ObjectInputStream ois = new ObjectInputStream(fis);
@SuppressWarnings("unchecked")
HashMap<Integer, Student> student = (HashMap<Integer, Student>) ois.readObject();
System.out.println("Student Data from File...");
System.out.println(student);
ois.close();
}
}