Java 7 read file to String

Before Java7, reading a file in Java and convert to a String, used to require a lot of code either using FileInputStream or BufferReader with looping conditions. Of course, we might have used this by writing as a Utility method or using util methods from Apache Commons or Google Collections.

Java 7 comes with a new approach to read files to String in one-liner code, simplifying the hassles of looping and terminating conditions so forth. Actual code to do this is

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

java 7 read file to string: source code for the above.

1)Source code

2)Output

3)Download

Leave a Comment