This post will discuss how to read all text from a file into a string in Java.

There are several ways to read the contents of a file into a string using Plain Java and third-party libraries such as Guava, Apache Commons IO, etc. Also, with the introduction of the Files class in Java 7, several static methods are included with each subsequent release that operate on files, directories, or other types of files. All these are discussed below in detail:

1. Using Java 7 (java.nio.file.Files.readAllBytes)

To read all the bytes from a file, we can use the readAllBytes() method, which takes the path to the file and returns a byte array containing the bytes read from the file. To get output in the string format, pass the byte array to the String constructor with a charset for decoding.

Download Code

2. Using Java 7 (java.nio.file.Files.readAllLines)

To read the contents of a file into a string, we can use the readAllLines() method, which takes the file’s path. It is overloaded to additionally accept the charset to be used for decoding.

This method is convenient to read all lines in a single operation but returns a list of strings and strips line terminators from the end of each line. We can easily handle this by joining each of the elements of the list with a line separator, as shown below:

Download Code

3. Using Java 8 (java.nio.file.Files.lines)

Java 8 introduced Files.lines() method, which can read all lines from a file as a stream. It takes the path to the file and overloaded it to accept the charset for decoding.

Like Files.readAllLines(), Files.lines() method doesn’t include line-termination characters, which can be handled explicitly, as shown below:

Download Code

4. Using Java 8 (java.io.BufferedReader.lines)

Java 8 also introduced BufferedReader.lines() method, which returns a stream of lines of text read from BufferedReader. This is demonstrated below using Stream:

Download Code

5. Using Java 11 (java.nio.file.Files.readString)

Java 11 introduced the Files.readString() method, which can read all characters from a file into a string. It takes a path to the file and overloaded to accept the charset used for decoding from bytes to characters.

Download Code

6. Using Guava’s Files.asCharSource()

Several third-party libraries provide utility methods for working with files. Guava provides the Files.asCharSource() method, which can be used to read file contents into a string using the given character set, as shown below:

Download Code

7. Using Guava’s Files.readLines()

If you prefer Google’s Guava library, you can use the Files.readLines(file, charset) method reads all the lines from a file and returns a mutable List. For an ImmutableList, use Files.asCharSource(file, charset).readLines(). Note that both these methods strip line-termination characters from the end of each line.

Download Code

8. Using Apache Commons IO

Like Guava library, Apache Commons IO FileUtils class provides the readFileToString() method that allows you to read the contents of any file into a string using the specified charset for converting Bytes from the file into characters.

Download Code

 
Alternatively, we can use IOUtils class from Apache Commons IO library that has the toString() method. It takes an InputStream and renders its contents as a String, as shown below:

Download Code

9. Using Scanner

We can also construct a Scanner that produces values scanned from the specified file. This is demonstrated below using the try-with-resources block to automatically takes care of scanner.close().

Download Code

10. Plain Java

In plain Java, we can open an input stream of bytes using InputStream and keep reading bytes of data from it into a byte array using its read() method. Finally, to get output in the string format, pass the byte array to the String constructor with a charset for decoding.

Download Code

 
Another solution in plain Java is to use the BufferedReader with InputStreamReader. Each invocation of the readLine() method would read bytes from the file, convert them into characters, and then return. The following solution uses a StringBuilder to form a string from file contents using a platform-dependent line separator.

Download Code

 
Another solution in plain Java is to use a FileReader to read the whole file into a character array, as shown below:

Download Code

That’s all about copying text from a file into a Java String.