# How to read a File to a String in Java

By Atta Ur Rehman Shah (https://attacomsian.com/about). Published Dec 11, 2019. Topics: Java.
Canonical URL: https://attacomsian.com/blog/java-read-file-to-string

> A quick tutorial to learn how to read the contents of a file into a string in Java.

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](https://attacomsian.com/blog/java-read-write-files-nio-api) (classes in `java.nio.*` package) for efficiently handling files in Java.

Here is an example:

```java
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:

```java
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:

```java
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:

```java
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](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java), you can also use it to convert a file into a string as shown below:

```java
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**:

```java
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](https://attacomsian.com/blog/java-files-readallbytes-example). Later, you can convert the byte array into a string as shown below:

```java
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](https://commons.apache.org/proper/commons-io/) library provides `FileUtils.readFileToString()` method to convert a file into a string as shown below:

```java
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:

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

For a [Gradle project](https://attacomsian.com/blog/install-gradle-on-ubuntu), add the following dependency to your `build.gralde` file:

```groovy
implementation 'commons-io:commons-io:2.6'
```

## Further Reading

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

* [Reading and Writing Files in Java](https://attacomsian.com/blog/java-read-write-files)
* [How to Read and Write Text Files in Java](https://attacomsian.com/blog/java-read-write-text-files)
* [How to Read and Write Binary Files in Java](https://attacomsian.com/blog/java-read-write-binary-files)
* [Reading and Writing Files using Java NIO API](https://attacomsian.com/blog/java-read-write-files-nio-api)
* [How to read a file line by line in Java](https://attacomsian.com/blog/how-to-read-a-file-line-by-line-in-java)
