How to convert Reader to File in Java

May 16, 2020 No comments Java IO Conversions Reader InputStream

1. Introduction

In this article, we will learn how to convert Reader to File with plain Java and libraries such as Guava and Apache Commons IO.

For more Java I/O-related articles, check the following links:

2. Reader to File in plain Java

Let's take a look at a pure Java solution:

package com.frontbackend.java.io.conversions.fromreader.tofile;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;

public class ReaderToFileInPlainJava {

    public static void main(String[] args) throws IOException {
        Reader reader = new StringReader("frontbackend.com");

        File file = new File("/tmp/output.txt");
        FileWriter fileWriter = new FileWriter(file);

        int charVal;
        while ((charVal = reader.read()) != -1) {
            fileWriter.append((char) charVal);
        }

        fileWriter.close();
    }
}

In this example, we read characters from the Reader and write them in the File using FileWriter.

3. Convert Reader to File using Guava library

The Guava library comes with a direct solution:

package com.frontbackend.java.io.conversions.fromreader.tofile;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import com.google.common.io.CharStreams;
import com.google.common.io.Files;

public class ReaderToFileUsingGuava {

    public static void main(String[] args) throws IOException {
        Reader reader = new StringReader("frontbackend.com");

        byte[] bytes = CharStreams.toString(reader)
                                  .getBytes(StandardCharsets.UTF_8);

        File file = new File("/tmp/output.txt");
        Files.write(bytes, file);
    }
}

First, we convert Reader to Byte Array, and then we used Files.write(bytes, file) method to write all read bytes to the File.

4. Read from Reader and write to File using Apache Commons IO library

The Apache Commons IO library provides the simplest solution:

package com.frontbackend.java.io.conversions.fromreader.tofile;

import java.io.File;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.nio.charset.StandardCharsets;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;

public class ReaderToFileUsingFileUtils {

    public static void main(String[] args) throws IOException {
        Reader reader = new StringReader("frontbackend.com");
        byte[] bytes = IOUtils.toByteArray(reader, StandardCharsets.UTF_8);

        File file = new File("/tmp/output.txt");
        FileUtils.writeByteArrayToFile(file, bytes);
    }
}

In this example first, we used IOUtils.toByteArray(..) method to convert Reader to byte[], and then we write that bytes to the File using another great utility - FileUtils.writeByteArrayToFile(..) method.

5. Conclusion

In this article, we presented various ways to convert Reader to a File in Java. The most transparent solution comes with Apache Commons IO library where we used two utility classes: IOUtils and FileUtils.

The examples used in this tutorial are available under our GitHub repository.

{{ message }}

{{ 'Comments are closed.' | trans }}