mirror of
https://github.com/smyalygames/kahoot-challenge-2025.git
synced 2025-09-13 20:12:21 +02:00
35 lines
1.0 KiB
Java
35 lines
1.0 KiB
Java
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<String> 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<String> 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;
|
|
}
|
|
}
|