Powered By Blogger

Tuesday 28 July 2015

FRESH, INTERESTING AND AMAZING UPDATE IN JAVA 8

HI GUYS

It's been a minute, (a while). I've been pretty busy doing a few things you do not need to know. However, this write-up explains how to read from files. This is a new update on the Java8 jdk and it's really an amazing  development.

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 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 file before printing them or returning them to caller. It's not just lambda expression, which is introduced in Java 8, there are many more goodies like this which are hidden behind aura of big features like lambdas and streams. You can read Java SE 8 for really impatient or Java 8 in Action to learn more about such hidden gems.

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 its not smooth. You need to first create a FileReader to read file, which uses platform's default character encoding for converting bytes to character. Than, 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 is no more lines to read. If you also want to count 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 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 are nothing but lines from File. There are two overloaded version of this method, one which accepts a character encoding and other which uses UTF-8 charset. Only problem with this method is that its 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 other runtime exception is occurred. You can see this Java 8 tutorial to see this method in action.


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 file or call count() method to calculate total number of lines form 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. 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 code inside try-with-resource statement to improve readability but that is must if you are doing it for production code.

HERE IS A CODE EXAMPLE.

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);
    }


}

No comments:

Post a Comment