How to convert InputStream to OutputStream in Java

In my previous article, 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:

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:

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 library provides the IOUtils.copy() method to copy data from InputStream to an OutputStream as shown below:

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:

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

For a Gradle project, add the following dependency to your build.gradle file:

implementation 'commons-io:commons-io:2.6'

Further Reading

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

✌️ Like this article? Follow me on Twitter and LinkedIn. You can also subscribe to RSS Feed.

You might also like...

Digital Ocean

The simplest cloud platform for developers & teams. Start with a $200 free credit.

Buy me a coffee ☕

If you enjoy reading my articles and want to help me out paying bills, please consider buying me a coffee ($5) or two ($10). I will be highly grateful to you ✌️

Enter the number of coffees below:

✨ Learn to build modern web applications using JavaScript and Spring Boot

I started this blog as a place to share everything I have learned in the last decade. I write about modern JavaScript, Node.js, Spring Boot, core Java, RESTful APIs, and all things web development.

The newsletter is sent every week and includes early access to clear, concise, and easy-to-follow tutorials, and other stuff I think you'd enjoy! No spam ever, unsubscribe at any time.