Share on Google+Share on Google+

Convert InputStream to File

In this section, you will learn to convert the InputStream to a File with an example.

Convert InputStream to File


Here we are showing how to convert an InputStream to File.

To do so first read the file as InputStream  using FileInputStream. Create FileOutputStream class object to retrieve the file from system for modification then convert the InputSteam into byte array before writing into file then pass this array to the input stream and finally the byte array into file. Now all the data  from InputStream is written into the desired file.


The code of the program is given below:

public class InputStreamToFile
  public static void main(String args[])
  File f=new File("");
  InputStream inputStream= new FileInputStream
  OutputStream out=new FileOutputStream(f);
  byte buf[]=new byte[1024];
  int len;
  System.out.println("\nFile is created........
  catch (IOException e){}

The output of the program is given below:

C:\rajesh\io>java InputStreamToFile
File is created.......................

Download this example.



Posted on: June 21, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: Convert InputStream to File   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
February 20, 2012

Thank you!
December 17, 2012

very useful this page
November 22, 2012

Mate, I don't know how you are, or how many people work on this blog, but any time I come here (Google) I find the answer I needed. You always keep the solution simple and direct. MANY THANKS!!!!!!!!!!!