Reading Entire File into String in Java

Java

In this tutorial, we’ll show you some examples of how to read the entire contents of a file into a string in Java.

What You’ll Need

File

This is the testing file for below Java examples.

C:\test-read-entire-file-into-string.txt

This is file test-read-entire-file-into-string.txt
This is Java example of reading entire content in a file into a string

Java 6 Example

This is the code snippet of reading a file into string in Java 6.

Java6ReadEntireFileExample.java

package com.chankok.io;

import java.io.FileReader;
import java.io.IOException;

public class Java6ReadEntireFileExample {

    public static void main(String[] args) {

        FileReader fr = null;

        try {
            StringBuilder sb = new StringBuilder();

            // Create an input stream
            fr = new FileReader("C:\\test-read-entire-file-into-string.txt");

            int data;
            // Read a character and append it to string builder one by one
            while ((data = fr.read()) != -1) {
                sb.append((char) data);
            }
            System.out.println(sb.toString());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                // Close the input stream
                if (fr != null) fr.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

}

Java 7 Example

This is the code snippet of reading a file into string in Java 7.

Java7ReadEntireFileExample.java

package com.chankok.io;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class Java7ReadEntireFileExample {

    public static void main(String[] args) {

        try {
            // Create a file path of the file
            Path filePath = Paths.get("C:\\test-read-entire-file-into-string.txt");

            // Reading all bytes in a file into a string
            String stringData = new String(Files.readAllBytes(filePath), StandardCharsets.UTF_8);
            System.out.println(stringData);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Note: Remember to set the character encoding (For example, UTF-8) when you create a string from bytes to avoid the unexpected character encoding issue.

Java 8 Example

This is the code snippet of reading a file into string in Java 8.

Java8ReadEntireFileExample.java

package com.chankok.io;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;

public class Java8ReadEntireFileExample {

    public static void main(String[] args) {

        try {
            // Create a file path of the file
            Path filePath = Paths.get("C:\\test-read-entire-file-into-string.txt");

            // Reading and joining all lines in a file into a string
            String stringData = Files.lines(filePath, StandardCharsets.UTF_8)
                .collect(Collectors.joining("\n"));
            System.out.println(stringData);

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}

Output

This is file test-read-entire-file-into-string.txt
This is Java example of reading entire content in a file into a string

Other Resources