# How to convert InputStream to OutputStream in Java

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

> A quick tutorial to learn how to convert an instance of InputStream into an OutputStream object in Java.

In my [previous article](https://attacomsian.com/blog/java-convert-inputstream-to-file), we looked at different ways to convert an `InputStream` instance to a file using Java. In this article, you'll learn how to transform an `InputStream` object into an `OutputStream` object.

## Convert InputStream to OutputStream using `InputStream.transferTo()`

In Java 9 or higher, you can use the `InputStream.transferTo()` method to copy data from `InputStream` to `OutputStream`. This method reads all bytes from this input stream and writes the bytes to the given output stream in the original order.

Here is an example:

```java
try (InputStream in = Files.newInputStream(Paths.get("input.txt"));
     OutputStream out = Files.newOutputStream(Paths.get("output.txt"))) {

    // convert input stream to output stream
    long length = in.transferTo(out);

    System.out.println("Bytes transferred: " + length);

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

## Convert InputStream to OutputStream using manual copy data

In Java 8 or below, you can manually copy data from an `InputStream` to an `OutputStream` object as shown below:

```java
try (InputStream in = Files.newInputStream(Paths.get("input.txt"));
     OutputStream out = Files.newOutputStream(Paths.get("output.txt"))) {

    int length;
    byte[] bytes = new byte[1024];

    // copy data from input stream to output stream
    while ((length = in.read(bytes)) != -1) {
        out.write(bytes, 0, length);
    }

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

## Convert InputStream to OutputStream using Apache Commons IO

The [Apache Commons IO](https://commons.apache.org/proper/commons-io/) library provides the `IOUtils.copy()` method to copy data from `InputStream` to an `OutputStream` as shown below:

```java
try (InputStream in = Files.newInputStream(Paths.get("input.txt"));
     OutputStream out = Files.newOutputStream(Paths.get("output.txt"))) {

    // convert input stream to output stream
    int bytesCopied = IOUtils.copy(in, out);

    System.out.println("Bytes Copied: " + bytesCopied);
} 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.gradle` 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)
