JAVA | Describe file output stream class with one example.

A file output stream is an output stream for writing data to a File.
FileOutputStream class is used to write bytes of data into a file. 
Constructor: (Any one)
FileOutputStream(File file)
This creates a file output stream to write to the file represented by the specified File object.
FileOutputStream(String name)
This creates an output file stream to write to the file with the specified name. 

Example:
import java.io.*;
class Fileoutputstream Example
{
public static void main(String a[]) throws IOException 
{
 File fin = new File("in.txt");
 File fout = new File("out.txt");
 FileInputStreamfis = null;
 FileOutputStreamfos = null;
 try {
  fis  =new FileInputStream(fin); 
  fos = new FileOutputStream(fout);
  intch;
  while((ch=fis.read())!=-1)
  {
  fos.write(ch);
  }
 } catch(Exception e)
{   
 }
 finally{
 if(fis != null)
{
 fis.close();
 }
if(fos != null)
{
 fos.close();
}
 }
}

Post a Comment

0 Comments