How to Write an InputStream to a File

This article shows a different ways of Writing InputStream to File in Java by using Apache Commons IO, Guava, and Core Java.

Overview

We often need to create files and add contents to it. For example, we may need to write String elements to a file or store the bytes received over the network to a file. In all such scenarios, we have the data in the form of InputStreams, which needs to be written to File.

This tutorials focuses on Converting InputStreams to a File in Java. We will write examples using using Apache Common IO, Guava, and finally using Core Java library.

InputStream to File using Apache Commons

The Apache Commons IO API is a handy utility library that provides quite useful abstractions for various File IO operations.

Next is an example of using Apache commons InputStream to File abstraction.

File outputFile = new File(outputPath + "output.txt"); FileUtils.copyInputStreamToFile(inputStream, outputFile);
Code language: Java (java)

First, we create a File instance pointing to the desired path on the disk. Next, we use the copyInputStreamToFile method to read bytes from InputStream and copy into the given file.

InputStream to File using Guava

Similar to the apache commons the Guava Library also provides various abstraction for File IO.

Next is Guava example of InputStream to File.

File outputFile = new File(outputPath + "output.txt"); FileOutputStream outputStream = new FileOutputStream(outputFile); ByteStreams.copy(inputStream, outputStream);
Code language: Java (java)

We created a FileOutputStream based on the output File instance. Then we used the utility class BytesStreams to copy contents of InputStream to OutputStream.

InputStream to File using Java

So far, we have covered the third party libraries. In this section we will cover how Core Java natively supports writing InputStream to File.

Using Java Legacy way

int size = inputStream.available(); byte[] bucket = new byte[size]; inputStream.read(bucket); try (FileOutputStream outputStream = new FileOutputStream(outputPath + "output.txt")) { outputStream.write(bucket); }
Code language: Java (java)

This option should be avoided or used only if the underlying data has a fixed length and if the underlying data can safely be fit into memory.

First, we create a bucket, which is a byte[] of the exact size as that of data. We read all the bytes from the input stream into the bucket and write the bucket onto a file output stream.

If we use the same logic on an input stream of a very large resource, we may run out of memory. Also, if the InputStream is based on an ongoing resource like reading a very large file over HTTP or tailing on a log file the available() method will only return the number of bytes it currently holds. In other words, we may not get the size of actual data and produce incorrect results.

If we are working with ongoing streams or large resource we can do the conversion iteratively by using a moderately sized bucket.

byte[] bucket = new byte[1024]; try (FileOutputStream outputStream = new FileOutputStream(outputPath + "output.txt")) { while ((inputStream.read(bucket, 0, bucket.length)) != -1) { outputStream.write(bucket); } }
Code language: Java (java)

Here, we create a bucket of 1024 bytes, which is used iteratively to copy data. In other words, during each iteration, 1024 bytes are read from the input stream and written on the output stream. The iteration finishes when no more bytes are left on the InputStream. For more on Large File Processing in Java, please visit Efficient Ways of Large File Processing in Java.

Using InputStream#transferTo

The transferTo method was introduced in Java SE 9, and provides abstraction over the logic that we have seen above.

try (FileOutputStream outputStream = new FileOutputStream(outputPath + "output.txt")) { inputStream.transferTo(outputStream); }
Code language: Java (java)

Using Files#copy

Finally, we will see how to use Files#copy method to Convert InputStream to File. Java introduced this option in Java SE 7.

File outputFile = new File(outputPath + "output.txt"); Files.copy(inputStream, outputFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
Code language: Java (java)

The copy option that we provided, will replace the output file if it exists already.

Summary

In this tutorial we covered a different ways of writing InputStream content to a File. We wrote a different examples of using Apache Commons IO, guava. Also, we covered a couple of Core Java methods to achieve the same. For more on Java, please visit Java Tutorials.