-
Notifications
You must be signed in to change notification settings - Fork 0
/
StudentReader.java
51 lines (44 loc) · 1.15 KB
/
StudentReader.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
package ws5;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;
public class StudentReader {
public static ArrayList<Student> getStudentArrayList() {
ArrayList<Student> students = new ArrayList<Student>();
try {
FileInputStream fis = new FileInputStream("students.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if (o instanceof ArrayList<?>) {
students = (ArrayList<Student>) o;
}
}
catch (ClassNotFoundException | IOException e) {
System.out.println(e.toString());
}
return students;
}
public static String getStudents() {
try {
FileInputStream fis = new FileInputStream("students.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
Object o = ois.readObject();
if (o instanceof ArrayList<?>) {
String str = "";
for (Object s : (ArrayList<?>) o) {
str += s.toString() + "\n";
}
fis.close();
return str;
}
else {
fis.close();
return "Incorrect data type in file.";
}
}
catch (ClassNotFoundException | IOException e) {
return e.toString();
}
}
}