Wednesday, September 23, 2015

how to read file into string in java 8

Many times you want to read contents of a file into String, but, unfortunately, it was not a trivial job in Java, at least not until JDK 1.7. In Java 8, you can read a file into String in just one line of code. Prior to the release of new File IO API, you have to write lot of boilerplate code e.g. open an input stream, convert that input stream into a Reader, and then wrap that into a BufferedReader and so on.

Reading File to String in Java

In order to understand the beauty of Java 7 way of reading the file into String, first, let's see how we used to do it on Java 1.5 and 6.

InputStream is = new FileInputStream("manifest.mf");
BufferedReader buf = new BufferedReader(new InputStreamReader(is));
        
String line = buf.readLine();
StringBuilder sb = new StringBuilder();
        
while(line != null){
   sb.append(line).append("\n");
   line = buf.readLine();
}
String fileAsString = sb.toString();
System.out.println("Contents : " + fileAsString);

You can see that it's not easy, you need to write a lot of unnecessary boilerplate code. This is even when we are just writing for the demo, forget about production quality code when you have to handle exceptions properly. Worth noting is that, in this example we are using platform's default character encoding, which is fine because manifest.mf has not contained any character other than ASCII, but it's not a safe way, if you don't know the encoding, by default use "UTF-8". Now let's see how you can read a file as String in JDK 1.7
String contents = new String(Files.readAllBytes(Paths.get("manifest.mf")));
System.out.println("Contents (Java 7) : " + contents);
You can see there is no more wrapping around different class, no more loop, no more handling of special condition, just a method call to read whole file as byte array and then create String from it. Just like our previous example, this is also using platform's default character encoding. Let's see how can we provide a custom character encoding of our choice:
String fileString = new String(Files.readAllBytes(Paths.get("manifest.mf")), StandardCharsets.UTF_8);
System.out.println("Contents (Java 7 with character encoding ) : " + fileString);
Now does it get any simpler with Java 8 Streams and lambda expression, well it does, as we have seen on my earlier article about how to read file in Java 8, its same as Java 7, how can you go less than one line, but yes you can use Stream and its lazy evaluation for your benefit :
Files.lines(Paths.get("manifest.mf"), StandardCharsets.UTF_8).forEach(System.out::println);

You should get familiar with new File API, it's really helpful to do efficient file IO in Java, as shown below: How to read file into String in Java 8
package thinkwithjava;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;


/**
 * Java Program to demonstrate different ways to loop over collection in 
 * pre Java 8 and Java 8 world using Stream's forEach method.
 * @author RD
 */
public class FileToStringJava8 {

    public static void main(String args[]) throws IOException {

        // How to read file into String before Java 7
        InputStream is = new FileInputStream("manifest.mf");
        BufferedReader buf = new BufferedReader(new InputStreamReader(is));
        
        String line = buf.readLine();
        StringBuilder sb = new StringBuilder();
        
        while(line != null){
            sb.append(line).append("\n");
            line = buf.readLine();
        }
        
        String fileAsString = sb.toString();
        System.out.println("Contents (before Java 7) : " + fileAsString);
        
        
        // Reading file into Stirng in one line in JDK 7
        String contents = new String(Files.readAllBytes(Paths.get("manifest.mf")));
        System.out.println("Contents (Java 7) : " + contents);
        
        
        
        // Reading file into String using proper character encoding
        String fileString = new String(Files.readAllBytes(Paths.get("manifest.mf")), StandardCharsets.UTF_8);
        System.out.println("Contents (Java 7 with character encoding ) : " + fileString);
        

        // It's even easier in Java 8
        Files.lines(Paths.get("manifest.mf"), StandardCharsets.UTF_8).forEach(System.out::println);
        
    }


}

That's all about how to read the file into String in Java. Though this is good for the small tasks where you need contents of file as String in your program, don't read a large file of few Gigabytes like that, otherwise your Java program will run out of memory, instead use InputStream. Also, always remember to use character encoding while converting binary data to character data. If you are unsure, just use UTF-8 as default.

No comments:

Post a Comment

Java garbage collection

In this post , we ’ ll take a look at how garbage collection works , why it ’ s important in Java , and how it works in...