-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataStore.java
More file actions
30 lines (23 loc) · 753 Bytes
/
DataStore.java
File metadata and controls
30 lines (23 loc) · 753 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
import java.io.*;
public class DataStore {
private static final String FILE = "bank.dat";
public static void save(Bank bank) {
try (ObjectOutputStream out =
new ObjectOutputStream(new FileOutputStream(FILE))) {
out.writeObject(bank);
} catch (IOException e) {
System.out.println("Error saving data.");
}
}
public static Bank load() {
File file = new File(FILE);
if (!file.exists())
return new Bank();
try (ObjectInputStream in =
new ObjectInputStream(new FileInputStream(FILE))) {
return (Bank) in.readObject();
} catch (Exception e) {
return new Bank();
}
}
}