# How to convert an InputStream to a File in Java

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

> A short article to learn how to convert an InputStream object to a file object in Java.

In this quick article, you'll learn how to convert an instance of `InputStream` to a file using Java. In Java, there are several ways to do this conversion as explained below.

## Using `Files.copy()` Method

In Java 7 or higher, you can use the `Files.copy()` method from Java's NIO API to copy an `InputStream` object to a file as shown below:

```java
try (InputStream stream = Files.newInputStream(Paths.get("input.txt"))) {

    // convert stream to file
    Files.copy(stream, Paths.get("output.txt"));

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

The above code will throw an error if the file already exists. To **replace the existing file**, you can use the below example code:

```java
try (InputStream stream = Files.newInputStream(Paths.get("input.txt"))) {

    // convert stream to file
    Files.copy(stream, Paths.get("output.txt"), StandardCopyOption.REPLACE_EXISTING);

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

## Using `OutputStream` Class

In Java 6 or below, you can use the `OutputStream` class to manually copy data from `InputStream` to a file as shown below:

```java
try (InputStream inputStream = new FileInputStream(new File("input.txt"));
     OutputStream outputStream = new FileOutputStream(new File("output.txt"))) {
        int length;
        byte[] bytes = new byte[1024];
        // copy data from input stream to output stream
        while ((length = inputStream.read(bytes)) != -1) {
            outputStream.write(bytes, 0, length);
        }

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

## Using Apache Commons IO

The [Apache Commons IO](https://commons.apache.org/proper/commons-io/) library provides `IOUtils.copyInputStreamToFile()` method to easily copy an instance of `InputStream` to a file as shown below:

```java
try (InputStream stream = Files.newInputStream(Paths.get("input.txt"))) {

    // convert input stream to file
    FileUtils.copyInputStreamToFile(stream, new File("output.txt"));

} 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)
