This post will discuss how to read the contents of a file line by line in Java.

There are several ways to read the contents of a file line by line in Java using BufferedReader, Files, Scanner, and RandomAccessFile class, and third-party libraries such as Guava and Apache Commons IO. These are discussed below in detail:

1. Using BufferedReader class

BufferedReader is a class that provides buffered reading of characters from a stream. It can wrap around any Reader object, such as FileReader, InputStreamReader, or StringReader, and read characters from it efficiently. BufferedReader has a method called readLine() that returns a String containing the contents of a line from the stream, or null if the end of the stream has been reached. To use BufferedReader to read a file line by line, we can do:

Download Code

 
The BufferedReader provides efficient reading of large files by buffering the characters in memory. In Java 8 and above, we can use the BufferedReader.lines() method to get a stream of lines of text read from BufferedReader. This is demonstrated below:

Download Code

2. Using Files class

Files is a utility class that provides various methods for working with files and directories. The Files class provides a convenient and concise method readAllLines() to read all the lines from a file into a List of Strings. It can also take a Charset argument to specify the character encoding of the file. Here is an example of using the Files to read a file line by line:

Download Code

 
Java 8 introduced lines() method in the Files class, which can read all lines from a file as a stream. It takes the path to the file and overloaded it to accept the charset for decoding.

Download Code

3. Using Scanner class

Another plausible way of reading a file in Java is to construct a Scanner that produces values scanned from the specified file. The Scanner class provides convenient methods for reading input from various sources. It has a method called hasNextLine() that returns true if there is another line in the input, and a method called nextLine() that returns the next line from the input. We can use Scanner to read a file line by line as follows:

Download Code

 
The Scanner can read input from various sources, such as keyboard, files, streams, or strings and parse different types of data. However, it may be slower than BufferedReader, as it performs more operations on the input, such as parsing and tokenizing.

4. Using RandomAccessFile class

RandomAccessFile is a class that allows reading and writing data from any position in a file. It can be used to access files in a random-access manner, rather than sequentially. RandomAccessFile has a method called readLine() that reads the next line of text from the file. It returns null if the end of the file has been reached. Here is an example of using the RandomAccessFile to read a file line by line:

Download Code

 
The RandomAccessFile supports different modes of operation and allows random access to any position in a file. However, it may be more complex and error-prone than the other methods, as it requires handling low-level details such as byte arrays, character encodings, and file pointers.

5. Using Guava Library

Guava Files class provides several utility methods for working with files. We can use the Files.readLines() method that reads all the lines from a file into a mutable list. It accepts two parameters: the file to read from and the charset used to decode the input stream. It throws an IOException if an I/O error occurs. To get an immutable list, we can use Files.asCharSource(file, charset).readLines() method instead. Here is an example of using the Files.readLines() method:

Download Code

6. Using Apache Commons IO

The Apache commons IO provides an utility method called FileUtils.readLines() that allows us to read the contents of a file line by line into a list of strings. It takes two parameters: a File object representing the file to read, and a String object representing the encoding to use. Here is an example of using the FileUtils.readLines() method:

Download Code

That’s all about reading the contents of a file line by line in Java.