Preparing for Java and Spring Boot Interview?

Join my Newsletter, its FREE

Tuesday, February 8, 2022

3 Ways to Read File line by line in Java 8? Examples

Java 8 has added a new method called lines() in the Files class which can be used to read a file line by line in Java. The beauty of this method is that it reads all lines from a file as Stream of String, which is populated lazily as the stream is consumed. So, if you have a huge file and you only read the first 100 lines then the rest of the lines will not be loaded into memory, which results in better performance. This is slightly different from the Files.readAllLines() method (which reads all lines into a List) because this method reads the file lazily, only when a terminal operation is called on Stream like forEach(), count(), etc. By using the count() method you can actually count a number of lines in files or the number of empty lines by filtering empty lines.

In fact, you can do a lot more than just reading content from a file, you can filter them on some criterion e.g. filter lines that do not start with a specific word, filter lines whose length is greater than 100, trim all lines to remove leading and trailing space, convert each line into uppercase or lowercase, etc.

In short, you can use different methods from class to process lines read from a file before printing them or returning them to the caller.  It's not just lambda expression, which is introduced in Java 8, there are many more goodies like this that are hidden behind the aura of big features like lambdas and streams.

Btw, if you are not familiar with new Java 8 features like Effectively final variable then I suggest you first go through a comprehensive and up-to-date Java course like one of these Java courses on this list of best Java courses on Udemy. It's also very affordable and you can buy in just $10 on Udemy sales which happen every now and then.

How to Read File line by line in Java 8? Example Tutorial

In this short example, I have discussed three ways to read a text file line by line in Java 1.8. My first example is about the classical approach of reading files line by line using BufferedReader. You can also use a Scanner in place of BufferedReader if you are using Java 1.5 but you can see that it's not smooth.

You need to first create a FileReader to read a file, which uses the platform's default character encoding for converting bytes to characters. Then, you need to wrap that inside BufferedReader to take advantage of the in-memory buffering and readLine() method of BufferedReader class. This method can be used to read files line by line, it returns null if there are no more lines to read.

If you also want to count a total number of lines or want to display line numbers along with each line, you can use a count variable as shown in our first example.  You can see, almost 9 to 10 lines of code are required to read a file line by line prior to Java 8.

There are a couple of ways to read a file line by line in Java 8 like by using Files.readAllLines() method, which returns a List of String, which is nothing but lines from File. There are two overloaded versions of this method, one which accepts a character encoding and the other which uses UTF-8 charset.

The only problem with this method is that it's not lazy like the next method, I am going to show you guys, but it also has an inherent advantage, it ensures that file is closed when all bytes have been read or an I/O error or another runtime exception occurs. You can see this Java 8 tutorial to see this method in action.

A better way to read a text file line by line in Java 8 is by using Files.lines() method which takes advantage of the Stream API introduced in Java 8. This method is lazy and only reads lines when some terminal operation is performed on Stream e.g. when you call forEach() method to display lines from the file or call count() method to calculate a total number of lines from a file.

This method also comes in two overloaded versions, one which accepts a given character encoding and the other which by default uses UTF-8 encoding to convert bytes to character from file. The only disadvantage of this method is that it doesn't ensure that file is closed once all lines are read.

The returned stream by this method encapsulates a Reader and if you don't want to rely on the operating system for disposing of file handlers, you make sure to call this method inside try-catch, try-finally, or try-with-resource block to ensure that the close() method is called once stream operation is completed. I have not wrapped the code inside the try-with-resource statement to improve readability but that is a must if you are doing it for production code.

Java 8 Example of Reading File line by line

Here is our sample Java program for reading a text file, actually manifest. mf file from Eclipse's project directory in Java 8. The first method is a classic way to read a file using BufferedReader, but the rest of the code demonstrates how Java 8 can help you not only read file line by line but also to do filtering, transformation, and many more things with powerful Java 8 Stream API

If you are wondering about the 3rd empty line from a file then it's worth remembering that it does contain an empty last line, you can check that by opening file in a text editor like Notepad++ or Wordpad.

import java.nio.file.Files;
import java.nio.file.Paths;

 * Java Program to demonstrate how to read a file line by line in Java 8
 * @author Javin Paul
public class Java8FileReader {

    public static void main(String args[]) throws IOException {
        // reading a file line by line before Java 8
        FileReader fr = new FileReader("");
        BufferedReader bufr = new BufferedReader(fr);
        int count = 1;
        String line = bufr.readLine();
        System.out.println("Old way of reading file line by line in Java : ");
        while(line != null){
            System.out.println(count + " : " + line);
            line = bufr.readLine(); count++;
        // reading file line by line in Java 8
        System.out.println("Reading file line by line using Files.lines() 
                              in Java 8");
        // You can do even better, you can read all lines
        // trim them and filter out all empty lines
        // before printing as shown in following example 
        System.out.println("Doing more things than just reading
                   file using Java 8 Streams");
        Files.lines(new File("").toPath())
                .map(s -> s.trim())
                .filter(s -> !s.isEmpty())
        // You can also filter line using String methods
        // e.g. print only lines which starts with "
        System.out.println("Printing lines which startswith );
                .map(s -> s.trim())
                .filter(s -> s.startsWith("))


Old way of reading file line by line in Java : 
1 : Manifest-Version: 1.0
2 : X-COMMENT: Main-Class will be added automatically by build
3 : 
Reading file line by line using Files.lines() in Java 8
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build

Doing more thing then just reading file using Java 8 Streams
Manifest-Version: 1.0
X-COMMENT: Main-Class will be added automatically by build
Printing lines which startswith <? from file
<?xml version="1.0" encoding="UTF-8"?>

You can see that in the first example all three lines are printed with line numbers. In the second example also I have printed all lines without any filtering or transformation, but in a subsequent example,  I have trimmed each line and filtered out empty lines, that's why you are seeing only two lines there. In the last example, I have only printed the line which is starting with an opening HTML tag '<'.

How to read file line by line in Java 8 using Stream examples

That's all about 3 ways to read files line by line in Java 8. There is no need to use BufferedReader or Scanner anymore, you can either use Files.readAllLines() if the file is small and you are not concerned about loading all lines in memory, or better use Files.lines() to read a text file line by line lazily. 

This method will only read lines when a terminal operation will be called on Stream like the forEach() method to print lines from a file.

It's also worth remembering that, Files.readAllLines() uses UTF-8 character encoding and ensures that the file is closed when all bytes are read or an I/O error or runtime exception occurred, but Files.lines() doesn't provide such guarantee. If you want to timely release resources make sure to call Files.lines() method inside try-with-resource statement.

If you want to learn more about new features in Java 1.8, I suggest you read Java SE 8 for Really Impatient By Cay S. Horstmann, one of the best books for learning Java 8. It also covers some significant enhancements from Java 1.7 release e.g. new File API, try with resource statements, and improved exception handling

Other Java tutorials and examples you may like
If you like this Java 8 tutorial and want to learn more about new features introduced in Java 8, don't forget to check out the following amazing Java 8 tutorials :
  • How to use Lambda Expression in Place of Anonymous class (read here)
  • 5 Good books to Learn Functional Programming with Java 8 [books]
  • Java 8 Comparator Example (see example)
  • How to do SQL like GROUP By in Java 8 (read more)
  • How to use the Default method in Java 8. (see here)
  • 10 Examples of Lambda expressions in Java 8? [examples]
  • FizzBuzz Solution in Java 8? [solution]
  • How to use Map function in Java 8 (see more)
  • 20 Stack and Queue Interview questions for Practice (questions)
  • Free Java 8 tutorials and Books (read the book)
  • Top 10 tutorials to Learn Java 8 (read here)
  • 20 Examples of new Date and Time API in Java 8 (examples)
  • Good time to become Java 8 Certified - 20% discount from Oracle [read more]
  • 5 Ways to convert Java 8 Stream to List in JDK 1.8? [solution]

Thanks for reading this article so far. If you like this Java tutorial for reading a file line by line then please share it with your friends and colleagues. If you have any questions or feedback then please drop a note. 

1 comment :

Anonymous said...

What is the performance benefit of using Files.lines() for reading a text file in Java? Is it faster than Scanner and BufferedReader?

Post a Comment