-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathFileManager.java
More file actions
34 lines (29 loc) · 929 Bytes
/
FileManager.java
File metadata and controls
34 lines (29 loc) · 929 Bytes
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
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Handles saving/loading the list of projects using Java serialization.
*/
public class FileManager {
private String fileName;
public FileManager(String fileName) {
this.fileName = fileName;
}
public List<Project> read() throws IOException, ClassNotFoundException {
File f = new File(fileName);
if (!f.exists()) {
return new ArrayList<>();
}
try (ObjectInputStream ois =
new ObjectInputStream(new FileInputStream(f))) {
Object obj = ois.readObject();
return (List<Project>) obj;
}
}
public void write(List<Project> projects) throws IOException {
try (ObjectOutputStream oos =
new ObjectOutputStream(new FileOutputStream(fileName))) {
oos.writeObject(projects);
}
}
}