Skip to content

Commit

Permalink
Add case when file does not exist.
Browse files Browse the repository at this point in the history
  • Loading branch information
YaxinJoy committed Mar 3, 2022
1 parent 70b54da commit 72cc8d7
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
import DukeTask.Task;
import DukeTask.ToDo;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.*;
import java.nio.file.Files;
import java.nio.file.Paths;

public class Storage {
/**
Expand Down Expand Up @@ -39,6 +38,12 @@ public void saveData() throws IOException {
* @throws IOException if the file/ file path does not exist.
*/
public void readData() throws IOException {
try{
BufferedReader in = new BufferedReader(new FileReader(".\\src\\DataSrc\\taskList.txt"));
} catch(IOException e){
System.out.println("Welcome new user!");
createFile("./src/DataSrc");
}
BufferedReader in = new BufferedReader(new FileReader(".\\src\\DataSrc\\taskList.txt"));
String taskLine;
while ((taskLine = in.readLine()) != null) {
Expand All @@ -63,4 +68,16 @@ public void readData() throws IOException {
in.close();
}

private void createFile(String path){
try {
File f = new File(path);
if(!f.exists()){
f.mkdirs();
}
Files.createFile(Paths.get("./src/DataSrc/taskList.txt"));
System.out.println("File created successfully!");
}catch(IOException e){
System.out.println("File created unsuccessfully!");
}
}
}

0 comments on commit 72cc8d7

Please sign in to comment.