Java IO Tutorial

How to convert InputStream to File in Java

Below are some Java examples of converting InputStream to a File.

  1. Plain Java – FileOutputStream
  2. Apache Commons IO – FileUtils.copyInputStreamToFile
  3. Java 7 – Files.copy
  4. Java 9 – InputStream.transferTo

1. Plain Java – FileOutputStream

This example downloads the google.com HTML page and returns it as an InputStream. And we use FileOutputStream to copy the InputStream into a File, and save it somewhere.

InputStreamToFile1.java

package com.mkyong.io.howto;

import java.io.*;
import java.net.URI;

public class InputStreamToFile1 {

    /**
     * The default buffer size
     */
    public static final int DEFAULT_BUFFER_SIZE = 8192;

    public static void main(String[] args) throws IOException {

        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {

            File file = new File("c:\\test\\google.txt");

            copyInputStreamToFile(inputStream, file);

        }

    }

    private static void copyInputStreamToFile(InputStream inputStream, File file)
            throws IOException {

        // append = false
        try (FileOutputStream outputStream = new FileOutputStream(file, false)) {
            int read;
            byte[] bytes = new byte[DEFAULT_BUFFER_SIZE];
            while ((read = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, read);
            }
        }

    }

}

2. Apache Commons IO

If there is Apache Commons IO in the project, we can use the FileUtils.copyInputStreamToFile to copy the InputStream into a File.

pom.xml

  <dependency>
      <groupId>commons-io</groupId>
      <artifactId>commons-io</artifactId>
      <version>2.7</version>
  </dependency>
InputStreamToFile2.java

package com.mkyong.io.howto;

import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;

public class InputStreamToFile2 {

    public static void main(String[] args) throws IOException {

        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {

            File file = new File("c:\\test\\google.txt");

            // commons-io
            FileUtils.copyInputStreamToFile(inputStream, file);

        }

    }

}

3. Java 1.7 – Files.copy

In Java 1.7, we can use the Files.copy to copy the InputStream to a Path.

InputStreamToFile3.java

  URI u = URI.create("https://www.google.com/");
  try (InputStream inputStream = u.toURL().openStream()) {

      File file = new File("c:\\test\\google.txt");

      // Java 1.7
      Files.copy(inputStream, file.toPath(), StandardCopyOption.REPLACE_EXISTING);

  }

4. Java 9 – InputStream.transferTo

In Java 9, we can use the new InputStream#transferTo to copy the InputStream to an OutputStream directly.

InputStreamToFile4.java

package com.mkyong.io.howto;

import java.io.*;
import java.net.URI;

public class InputStreamToFile4 {

    public static void main(String[] args) throws IOException {

        URI u = URI.create("https://www.google.com/");
        try (InputStream inputStream = u.toURL().openStream()) {

            File file = new File("c:\\test\\google.txt");

            // Java 9
            copyInputStreamToFileJava9(inputStream, file);

        }

    }

    // Java 9
    private static void copyInputStreamToFileJava9(InputStream input, File file)
        throws IOException {

        // append = false
        try (OutputStream output = new FileOutputStream(file, false)) {
            input.transferTo(output);
        }

    }

}

5. Convert File to InputStream

We can use FileInputStream to convert a File to an InputStream.


  File file = new File("d:\\download\\google.txt");
  InputStream inputStream = new FileInputStream(file);

Download Source Code

$ git clone https://github.com/mkyong/core-java

$ cd java-io/how-to

References

About Author

author image
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter. If you like my tutorials, consider make a donation to these charities.

Comments

Subscribe
Notify of
4 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Martin
8 years ago

Thanks buddy! Helped a lot!

Gerardo
10 years ago

Dude, your code save my life lol, Greetings.

Ravi.S
11 years ago

Thats great, thank you very much!

Angel
11 years ago

Omg, this is amazing;
You helped me like 4 times with spring,springmvc and maven, and now this;
I admire you;
thank you really