-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectIOStream.java
More file actions
113 lines (95 loc) · 3.77 KB
/
Copy pathObjectIOStream.java
File metadata and controls
113 lines (95 loc) · 3.77 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
package io_nio2.io;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.List;
public class ObjectIOStream {
public static void main(String[] args) {
List<Person> people = List.of(
new Person("Alan", 25),
new Person("Peter", 25),
new Person("Michel", 25),
new Person("Lan", 25),
new Person("May", 25),
new Person("Keran", 25),
new Person("Trump", 25)
);
write("src/resource/file/Output.dat", people);
List<Person> read = read("src/resource/file/Output.dat");
read.forEach(System.out::println);
System.out.println("---------------------------");
write("src/resource/file/ObjectOutput.dat", people.get(0));
Person person = readPerson("src/resource/file/ObjectOutput.dat");
System.out.println(person);
}
public static void write(String filePath, List<Person> people) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
oos.writeObject(people);
} catch (IOException e) {
throw new RuntimeException("Error writing to file", e);
}
}
public static void write(String filePath, Person person) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
oos.writeObject(person);
} catch (IOException e) {
throw new RuntimeException("Error writing to file", e);
}
}
public static void write(String filePath, Object ob) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath))) {
oos.writeObject(ob);
} catch (IOException e) {
throw new RuntimeException("Error writing to file", e);
}
}
@SuppressWarnings("unchecked")
public static List<Person> read(String filePath) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
if(ois.readObject() instanceof List list) {
if (!list.isEmpty() && list.get(0) instanceof Person) {
return (List<Person>) list;
}
}
throw new RuntimeException("Content invalid");
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("Error reading to file", e);
}
}
public static Person readPerson(String filePath) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
if (ois.readObject() instanceof Person person) {
return person;
}
throw new RuntimeException("Content invalid");
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("Error reading to file", e);
}
}
public static Object readObject(String filePath) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath))) {
return ois.readObject();
} catch (IOException | ClassNotFoundException e) {
throw new RuntimeException("Error reading to file", e);
}
}
public static class Person implements Serializable {
private static final long serialVersionUID = 1L;
String name;
int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
}