This example will demonstrate how to read a text file which is a variation of text file into an array list. Using the popular song "You are my sunshine" lyrics stored in a text file we will use various techniques of reading the file in java using a buffered reader, file scanner, java 8 file stream, google guava and apache commons.
Setup
File constant
File contents
Straight up Java
Buffered reader
Using a bufferedreader we will read a text file in java loading each line into an arraylist.
File scanner
Using a scanner we will read a text file in java and while there is more lines we will add it to an arraylist.
Java 8
We will use the Files utility class introduced in java 7 and call lines which will read all lines of a file as a stream. Then we will convert the stream to an arralist.
Google Guava
Using guava we will read a text file.
Apache Commons
Using apache commons we will read the context of a text file into an arraylist.