-
Notifications
You must be signed in to change notification settings - Fork 0
/
Database.java
75 lines (61 loc) · 1.73 KB
/
Database.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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.company;
import java.io.*;
import java.util.ArrayList;
public class Database {
public ArrayList<String> content;
private final String databaseName;
public ArrayList<String> getContent() {
return content;
}
public Database(String databaseName) {
this.databaseName = databaseName;
this.content = readDatabase(databaseName);
}
public void saveDatabase(){
try
{
FileOutputStream fos = new FileOutputStream(databaseName);
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(content);
oos.close();
fos.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
private ArrayList<String> readDatabase(String databaseName){
ArrayList<String> content;
try
{
FileInputStream fis = new FileInputStream(databaseName);
ObjectInputStream ois = new ObjectInputStream(fis);
content = (ArrayList) ois.readObject();
if (content == null){
content = new ArrayList<>();
}
ois.close();
fis.close();
}
catch (IOException ioe)
{
System.out.println("Database was null, creating a new one");
content = new ArrayList<>();
saveDatabase();
return content;
}
catch (ClassNotFoundException c)
{
System.out.println("Class not found");
c.printStackTrace();
System.out.println("WARNING: DATABASE RETURNED NULL");
return null;
}
return content;
}
@Override
public String toString() {
return content.toString();
}
}