How to convert InputStream to File in Java
Published: December 20, 2009 , Updated: August 9, 2011 , Author: mkyong
Here’s a example to show you how to convert InputStream to a file. The concept is quite straightforward, just get all content via InputStream and write it tofile via FileOutputStream.
package com.mkyong.core; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; public class App { public static void main(String[] args) { try { // read this file into InputStream InputStream inputStream = new FileInputStream("c:\\file.xml"); // write the inputStream to a FileOutputStream OutputStream out = new FileOutputStream(new File("c:\\newfile.xml")); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { out.write(bytes, 0, read); } inputStream.close(); out.flush(); out.close(); System.out.println("New file created!"); } catch (IOException e) { System.out.println(e.getMessage()); } } }
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~