diff --git a/src/ReadFile.java b/src/ReadFile.java new file mode 100644 index 0000000..d947330 --- /dev/null +++ b/src/ReadFile.java @@ -0,0 +1,34 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class ReadFile { + /** + * Opens a file and returns a list of all the lines in the file + * @param filename the name of the file with, only allowing a .txt extension + * @return The contents of the file + */ + public List open(String filename) throws IllegalArgumentException, FileNotFoundException { + // Check that the filetype is correct, otherwise, throw IllegalArgumentException + if (!filename.endsWith(".txt")) { + throw new IllegalArgumentException("The filename is not a .txt format"); + } + + // Open the file that was defined + File file = new File(filename); + Scanner reader = new Scanner(file); + + // List for all the separate lines on the database + List data = new ArrayList<>(); + + // Go through all the lines in the file and append them to the list `data` + while (reader.hasNextLine()) { + String currentData = reader.nextLine(); + data.add(currentData); + } + + return data; + } +}