Below are some Java examples of converting InputStream
to a File
.
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.favtuts.io.howto; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; 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("/home/tvt/workspace/favtuts/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.favtuts.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("/home/tvt/workspace/favtuts/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("/home/tvt/workspace/favtuts/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.favtuts.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("/home/tvt/workspace/favtuts/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("/home/tvt/workspace/favtuts/google.txt");
InputStream inputStream = new FileInputStream(file);
Download Source Code
$ git clone https://github.com/favtuts/java-core-tutorials-examples
$ cd java-io/howto