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

Java 8 has added a new method called lines() in 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 first 100 lines then rest of the lines will not be loaded into memory, which results in better performance. This is slightly different than 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 e.g. forEach(), count() etc. By using count() method you can actually count a number of lines in files or number of empty lines by filtering empty lines.

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

In short, you can use different methods from java.util.stream.Streams 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 which are hidden behind the aura of big features like lambdas and streams.

You can also read Java SE 8 for really impatient or Java 8 in Action to learn more about such hidden gems.

efficient way to read a file in Java 8



How to Read File line by line in Java 8

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 file line by line using BufferedReader. You can also use 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 platform's default character encoding for converting bytes to characters. Then, you need to wrap that inside BufferedReader to take advantage of in-memory buffering and readLine() method of BufferedReader class. This method can be used to read file 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 is required to read a file line by line prior to Java 8.


There are the couple of ways to read file line by line in Java 8 e.g. 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 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 take advantage of 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 version, one which accept a given character encoding and 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 operating system for disposing file handlers, you make sure to call this method inside try-catch, try-finally or try-with-resource block to ensure that close() method is called once stream operation is completed. I have not wrapped the code inside try-with-resource statement to improve readability but that is must if you are doing it for production code.

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



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 rest of the code demonstrate 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 manifest.mf file then it's worth remembering that it does contain an empty last line, you can check that by opening manifest.mf file in a text edition like Notepad++ or Wordpad.

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
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("manifest.mf");
        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++;
        }
        
        bufr.close();
        
        // reading file line by line in Java 8
        System.out.println("Reading file line by line using Files.lines() in Java 8");
        Files.lines(Paths.get("manifest.mf")).forEach(System.out::println);
        
        // 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("manifest.mf").toPath())
                .map(s -> s.trim())
                .filter(s -> !s.isEmpty())
                .forEach(System.out::println);
        
        // You can also filter line using String methods
        // e.g. print only lines which starts with "
        System.out.println("Printing lines which startswith );
        Files.lines(Paths.get("build.xml"))
                .map(s -> s.trim())
                .filter(s -> s.startsWith("))
                .forEach(System.out::println);
    }


}

Output
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 number. In the second example also we 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 '<'.


That's all about 3 ways to read file line by line in Java 8. There is no need to use BufferedReader or Scanner any more, 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 e.g. forEach() method to print lines from a file.


It's also worth remembering that, Files.readAllLines() uses UTF-8 character encoding and ensures that 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 resource 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 book 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.

How to read file line by line in Java8



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 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 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)
  • What is effectively final variable in Java 8? [answer]
  • Free Java 8 tutorials and Books (read 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]


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