Java 7 one-liner to read file into string

24 March 2014, by: Arjan Tijms

Reading in a file in Java used to require a lot of code. Various things had to wrapped, loops with weird terminating conditions had to be specified and so forth.

In Java 7 we can do a lot better. The actual code to do the reading is just:

String content = new String(readAllBytes(get("test.txt")));

As a full program that echos back the file’s content it looks like this:

import static java.lang.System.out;
import static java.nio.file.Files.readAllBytes;
import static java.nio.file.Paths.get;
 
public class Test {
    public static void main(String[] args) throws Exception {
	out.println(new String(readAllBytes(get("test.txt"))));
    }
}

Of course it we want to be careful that we don’t load a few gigabytes into memory and if we want to pay attention to the character set (it’s the platform default now) we need a little more code, but for quick and dirty file reading this should do the trick.

As a bonus, a version in Scala using the same JDK 7 APIs contributed by my fellow office worker Mark van der Tol:

import java.nio.file.Files.readAllBytes
import java.nio.file.Paths.get
 
object Main extends App {
    println(new String(readAllBytes(get("test.txt"))))
}

Arjan Tijms

5 comments to “Java 7 one-liner to read file into string”

  1. ppearcy says:

    The code above potentially has a common issue and it is so annoying that Java (and most other languages) allow an implicit byte[] -> String conversion with the Systems default code page.

    This means that your code is now dependent on an operating system level setting that you don’t control.

    TL;DR – Always be explicit on byte[] String conversions. Always explicitly set your charsets, even if the silly language doesn’t require it.

  2. Daniel says:

    “Reading in a file in Java used to require a lot of code.”

    Nope. From
    http://stackoverflow.com/questions/3402735/what-is-simplest-way-to-read-a-file-into-string

    String content = new Scanner(new File(“filename”)).useDelimiter(“\\Z”).next();

  3. Arjan Tijms says:

    This means that your code is now dependent on an operating system level setting that you don’t control.

    I know, so I mentioned this in the article. Just wanted it to be as short as possible. But indeed, for anything but quick experiments and proto-typing you should specify the char set.

    It would be great if Java just had UTF-8 as its default. I did notice that some newer methods in JDK 8 start having this as their default, but the older stuff has remained unchanged.

  4. Arjan Tijms says:

    String content = new Scanner(new File(“filename”)).useDelimiter(“\\Z”).next();

    Hey, wow! That’s actually kinda neat 😉 It’s a bit more mental overhead to remember it correctly I guess, but you’re right. It most definitely IS a one-liner. Thanks!

  5. Ajinkya says:

    I like this kind of tips, time saver for me. Keep it coming

Type your comment below:

Time limit is exhausted. Please reload CAPTCHA.

css.php best counter