FWIW, if you are using Java8, you can make use of the the stream APIs, as shown below
public static void main(String[] args) throws IOException {
String filename = "test.txt"; // Put your filename
List fileLines = new ArrayList<>();
// Make sure to add the "throws IOException" to the method
Stream<String> fileStream = Files.lines(Paths.get(filename));
fileStream.forEach(fileLines::add);
}
I've a main
method here, just put this in your method and add the throws
statement or a try-catch
block
You also could convert the above to a one liner as
((Stream<String>)Files.lines(Paths.get(filename))).forEach(fileLines::add);