Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Level-7. Save #2

Merged
merged 1 commit into from
Jan 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data/duke.txt
Binary file not shown.
78 changes: 78 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;

Expand All @@ -10,6 +11,7 @@ enum Command {
static String input;
static String[] tokens;
static ArrayList<Task> list;
static String path = "./data/duke.txt";


public static void main(String[] args) {
Expand All @@ -21,6 +23,7 @@ public static void main(String[] args) {
Command command;
int index;
Task t;
loadFile();
while (true) {
try {
input = scanner.nextLine();
Expand Down Expand Up @@ -114,6 +117,81 @@ private static void addTask(Task t) {
list.add(t);
System.out.println("Got it. I've added this task:\n" + t);
System.out.println("Now you have " + list.size() + " task" + (list.size() == 1 ? "" : "s") + " in the list.");
saveFile();
}

private static void loadFile() {
FileInputStream fi = null;
ObjectInputStream oi = null;
File file = null;
try {
file = new File(path);
if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
fi = new FileInputStream(file);
oi = new ObjectInputStream(fi);
while (true) {
Task t = (Task) oi.readObject();
list.add(t);
}
} catch (EOFException e) {

} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream: " + e.getMessage());
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
if (oi != null) {
oi.close();
}
if (fi != null) {
fi.close();
}
} catch (IOException ex) {

}
}
}

private static void saveFile() {
FileOutputStream fi = null;
ObjectOutputStream oi = null;
File file = null;
try {
file = new File(path);
if (file.exists()) {
file.delete();
}
file.getParentFile().mkdirs();
file.createNewFile();
fi = new FileOutputStream(file);
oi = new ObjectOutputStream(fi);
for (Task t : list) {
oi.writeObject(t);
}
} catch (EOFException e) {

} catch (FileNotFoundException e) {
System.out.println("File not found");
} catch (IOException e) {
System.out.println("Error initializing stream: " + e.getMessage());
try {
if (oi != null) {
oi.close();
}
if (fi != null) {
fi.close();
}
} catch (IOException ex) {

}
}
}

}
3 changes: 2 additions & 1 deletion src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class Task {
public class Task implements java.io.Serializable {
protected String description;
protected boolean isDone;

Expand All @@ -19,4 +19,5 @@ public boolean markAsDone() {
public String toString() {
return "[" + this.getStatusIcon() + "] " + this.description;
}

}