In Java, there are a lot of ways to read a file into a string. In this article, we will look at 5 different methods that can be used to convert a file into a string variable in Java.

1. Using Files.readString() Method

In Java 11 or higher, you can use the Files.readString() method to easily convert any text file to a string. This static method is a part of Java NIO API (classes in java.nio.* package) for efficiently handling files in Java.

Here is an example:

try {
    // read file into string
    String contents = Files.readString(Path.of("input.txt"));

    // print string
    System.out.println(contents);

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

To specify the file character encoding, you can pass a second parameter to Files.readString() method:

try {
    // read file into string
    String contents = Files.readString(Path.of("input.txt"), StandardCharsets.UTF_8);

    // print string
    System.out.println(contents);

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

2. Using Files.lines() Method

By using Files.lines() method in Java 7 or higher, we can read a file to a Stream and then convert it into a string as shown below:

try (Stream<String> stream = Files.lines(Paths.get("input.txt"))){

    // convert stream into a string
    String contents = stream.collect(Collectors.joining(System.lineSeparator()));

    // print string
    System.out.println(contents);

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

You can also specify a file character encoding like below:

try (Stream<String> stream = Files.lines(Paths.get("input.txt"),
        StandardCharsets.UTF_8)){

    // convert stream into a string
    String contents = stream.collect(Collectors.joining(System.lineSeparator()));

    // print string
    System.out.println(contents);

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

3. Using Files.readAllLines() Method

In Java 7+, Files.readAllLines() can be used to read a file into a List<String> object. Apart from using this method for reading a file line by line in Java, you can also use it to convert a file into a string as shown below:

try {
    // read file to list
    List<String> lines = Files.readAllLines(Paths.get("input.txt"));

    // convert list into a string
    String contents = lines.stream().collect(Collectors.joining(System.lineSeparator()));

    // print string
    System.out.println(contents);

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

Files.readAllLines() also accepts an optional second parameter to specify the file character encoding:

try {
    // read file to list
    List<String> lines = Files.readAllLines(Paths.get("input.txt"), StandardCharsets.UTF_8);

    // convert list into a string
    String contents = lines.stream().collect(Collectors.joining(System.lineSeparator()));

    // print string
    System.out.println(contents);

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

4. Using Files.readAllBytes() Method

In Java 7 or higher, you can also use Files.readAllBytes() to read a file into an array of bytes. Later, you can convert the byte array into a string as shown below:

try {
    // read all bytes
    byte[] bytes = Files.readAllBytes(Paths.get("input.txt"));

    // convert bytes to string
    String content = new String(bytes, StandardCharsets.UTF_8);

    // print contents
    System.out.println(content);

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

5. Using Apache Commons IO

The Apache Commons IO library provides FileUtils.readFileToString() method to convert a file into a string as shown below:

try {
    // read file into string
    String contents = FileUtils.readFileToString(new File("input.txt"), 
            StandardCharsets.UTF_8);

    // print string
    System.out.println(contents);

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

Don't forget to include Apache Commons IO dependency to your Maven's project pom.xml file:

<dependency>
    <groupId>commons-io</groupId>
    <artifactId>commons-io</artifactId>
    <version>2.6</version>
</dependency>

For a Gradle project, add the following dependency to your build.gralde file:

implementation 'commons-io:commons-io:2.6'

Further Reading

You may be interested in other Java I/O articles:

✌️ Like this article? Follow @attacomsian on Twitter. You can also follow me on LinkedIn and DEV. Buy me a coffee (cost $3)

Need help to start a new Spring Boot or MEAN stack project? I am available for contract work. Hire me to accomplish your business goals with engineering and design. Let’s talk about your project: hi@attacomsian.com.