From 33d1029c59d35e9a6c9647a83bca42f153bca8c6 Mon Sep 17 00:00:00 2001 From: Benjamin Lau Date: Tue, 28 Jan 2020 16:44:02 +0800 Subject: [PATCH] Level-7. Save --- data/duke.txt | Bin 0 -> 145 bytes src/main/java/Duke.java | 78 ++++++++++++++++++++++++++++++++++++++++ src/main/java/Task.java | 3 +- 3 files changed, 80 insertions(+), 1 deletion(-) create mode 100644 data/duke.txt diff --git a/data/duke.txt b/data/duke.txt new file mode 100644 index 0000000000000000000000000000000000000000..73e3e98133096e24a24cd0d1a646e56b25d868b0 GIT binary patch literal 145 zcmZ4UmVvdnh=C;}-zEQO(!}ty4=wbV7#J!*LW#xM8Yc}cipp98I%7}zq4UGno% zeHge?Qj3#|G7CyF^Ycm=gnY6R%M$f-67$magG-7s^U|#=3K&Wl7!!d8v$~e0=9RE% o*_d2q*u~7m!05xklvq;8P{+Vj0U{ZJ3fUZ;oPnr>ff+~w01>w;0{{R3 literal 0 HcmV?d00001 diff --git a/src/main/java/Duke.java b/src/main/java/Duke.java index 4ac25a9d62..17dceb58d6 100644 --- a/src/main/java/Duke.java +++ b/src/main/java/Duke.java @@ -1,3 +1,4 @@ +import java.io.*; import java.util.ArrayList; import java.util.Scanner; @@ -10,6 +11,7 @@ enum Command { static String input; static String[] tokens; static ArrayList list; + static String path = "./data/duke.txt"; public static void main(String[] args) { @@ -21,6 +23,7 @@ public static void main(String[] args) { Command command; int index; Task t; + loadFile(); while (true) { try { input = scanner.nextLine(); @@ -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) { + + } + } } } diff --git a/src/main/java/Task.java b/src/main/java/Task.java index 31eb1ed68b..789f719664 100644 --- a/src/main/java/Task.java +++ b/src/main/java/Task.java @@ -1,4 +1,4 @@ -public class Task { +public class Task implements java.io.Serializable { protected String description; protected boolean isDone; @@ -19,4 +19,5 @@ public boolean markAsDone() { public String toString() { return "[" + this.getStatusIcon() + "] " + this.description; } + } \ No newline at end of file