How to convert InputStream to File in Java
Written on December 20, 2009 at 1:53 am by
mkyong
Here’s a example to show how to convert InputStream to a file. The concept is quite straightforward, just get all content from input stream and write it to a FileOutputStream.
package com.steve.indexing; 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()); } } }
Oracle Magazine (Free)
Oracle Magazine contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more. Oracle (NASDAQ: ORCL) is the world\'s largest enterprise software company.
Publisher : Oracle Corporation


