Skip to content

Commit

Permalink
Updated Storage: Deal with absent text file in JAR
Browse files Browse the repository at this point in the history
  • Loading branch information
RyanTDL committed Mar 8, 2024
1 parent 037a9cd commit 500a0b8
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ bin/
/text-ui-test/ACTUAL.TXT
/text-ui-test/EXPECTED-UNIX.TXT
*.txt
*.jar
30 changes: 19 additions & 11 deletions src/main/java/jake/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package jake.storage;

import java.util.Scanner;
import java.io.File;
import java.io.FileNotFoundException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import jake.task.TaskList;
Expand Down Expand Up @@ -41,21 +43,27 @@ public void saveTasks(TaskList tasks) {

/**
* Loads up all previous tasks saved in tasks.txt into the new TaskList.
* If file is not found, print an error message.
* If file is not found, create a new file.
*
* @param task TaskList of all previous tasks saved within tasks.txt.
*/
public void loadTasks(TaskList tasks) {
try {
File savedFile = new File(filePath);
Scanner savedFileScanner = new Scanner(savedFile);
while (savedFileScanner.hasNext()) {
String userInput = savedFileScanner.nextLine();
char taskType = userInput.charAt(1);
tasks.addSavedTask(userInput, taskType);
Path path = Paths.get(filePath);
if (!Files.exists(path)) {
Files.createDirectories(path.getParent());
Files.createFile(path);
} else {
File savedFile = new File(filePath);
Scanner savedFileScanner = new Scanner(savedFile);
while (savedFileScanner.hasNext()) {
String userInput = savedFileScanner.nextLine();
char taskType = userInput.charAt(1);
tasks.addSavedTask(userInput, taskType);
}
savedFileScanner.close();
}
savedFileScanner.close();
} catch (FileNotFoundException e) {
} catch (IOException e) {
System.out.printf(MESSAGE_INVALID_FILEPATH, e.getMessage());
}
}
Expand Down

0 comments on commit 500a0b8

Please sign in to comment.