Home Java Java-conversion Convert InputStream to File



Convert InputStream to File
Posted on: June 21, 2007 at 12:00 AM
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:

import java.io.*;
public class InputStreamToFile
  {
  public static void main(String args[])
  {
  try
  {
  File f=new File("outFile.java");
  InputStream inputStream= new FileInputStream
(
"InputStreamToFile.java");
  OutputStream out=new FileOutputStream(f);
  byte buf[]=new byte[1024];
  int len;
  while((len=inputStream.read(buf))>0)
  out.write(buf,0,len);
  out.close();
  inputStream.close();
  System.out.println("\nFile is created........
..........................."
);
  }
  catch (IOException e){}
  }
  }

The output of the program is given below:

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

Download this example.

Related Tags for Convert InputStream to File:
cfilearrayclassinputobjectconvertiooutputstreamsystemteamreadusingbyteintthiscreateinputstreamshowifforiefinalwritingtofinallysheilitputtriemodificationoutputstreamfromfirstinmodpassasmntouttrcajadclretrieveemallinputshowintoobjmodicatfileinputstreamdosysssobeforeatisirllrayfileoutputstreameaandarstrrttstreamwingssriteathshostatifileoutputfileinputfinjeicaicaoutputsndonomonp


More Tutorials from this section

Ask Questions?    Discuss: Convert InputStream to File   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.