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
private static final String FILE_PATH = "youaremysunshine.txt";
File contents
"You Are My Sunshine"
You are my sunshine, my only sunshine
You make me happy when skies are grey
You never know, dear, how much I love you
Please don't take my sunshine away
...
Please don't take my sunshine away
Straight up Java
Buffered reader
Using a bufferedreader we will read a text file in java loading each line into an arraylist.
@Test
public void read_file_with_bufferedreader() {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_PATH);
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
List<String> lines = new ArrayList<String>();
String line = null;
try {
while ((line = reader.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
logger.error(e);
} finally {
try {
reader.close();
} catch (IOException e) {
logger.error(e);
}
}
assertTrue(lines.size() == 28);
}
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.
@Test
public void read_text_file_with_scanner () {
InputStream in = this.getClass().getClassLoader().getResourceAsStream(FILE_PATH);
ArrayList<String> lines = new ArrayList<String>();
Scanner fileScanner = new Scanner(new InputStreamReader(in));
while (fileScanner.hasNextLine()){
lines.add(fileScanner.nextLine());
}
assertTrue(lines.size() == 28);
}
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.
@Test
public void read_text_file_with_java8() throws IOException {
String filePathAsString = this.getClass().getClassLoader()
.getResource(FILE_PATH).getFile();
Path filePath = Paths.get(filePathAsString);
List<String> lines = java.nio.file.Files.lines(filePath).collect(
Collectors.toList());
assertTrue(lines.size() == 28);
}
Google Guava
Using guava we will read a text file.
@Test
public void read_text_file_with_google_guava () {
String filePath = this.getClass().getClassLoader().getResource(FILE_PATH).getFile();
List<String> lines = null;
try {
lines = Files.readLines(new File(filePath), Charsets.UTF_8);
} catch (IOException e) {
logger.error(e);
}
assertTrue(lines.size() == 28);
}
Apache Commons
Using apache commons we will read the context of a text file into an arraylist.
@Test
public void read_file_with_apache_commons () {
String filePath = this.getClass().getClassLoader().getResource(FILE_PATH).getFile();
List<String> lines = null;
try {
lines = FileUtils.readLines(new File(filePath));
} catch (IOException e) {
logger.error(e);
}
assertTrue(lines.size() == 28);
}