Thursday, 1 September 2016

ByteStream : FileInputStream and FileOutputStream

Byte Stream

It handles I/O of raw binary data.

FileInputStream and FileOutputStream:
However in java there are many classes available for input/output byte streaming,but the most frequently used classes are FileInputStream and FileOutputStream and these are used widely for file handling. These classes use Byte stream to perform input and output of 8 bit bytes.
Hence for reading and writing streams of raw bytes we should use these class and for reading streams of characters we should use FileReader and FileWriter

FileOutputStream:
This class is used to write the data to a destination file
Example 1:
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package filehandlingo;

import java.io.*;

/**
 *
 * @author shashank
 */
public class FileHandlingO {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        FileOutputStream fo = null;
        try
        {
         fo = new FileOutputStream("fileoutput.txt");
        String s = "my name is Shashank Bhatnagar--";
          byte[] b = s.getBytes();
            fo.write(b);
            fo.close();
            System.out.println("File has been updated , please check ");
        }
       
        finally{
            if(fo!=null)
            fo.close();
        }
    }
   
}
FileInputStream:
This class is used to read the data from a Source file.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package filehandlingi;

/**
 *
 * @author shashank
 */
import java.io.*;
public class FileHandlingI {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        FileInputStream fs = null;
        int i =0;
        int available = 0;
        try
        {
         fs = new FileInputStream("C://Users//shabhatn//Documents//NetBeansProjects//FileHandlingO//fileoutput.txt");
         while((i=fs.read())!=-1)
         {
           available =   fs.available();
          
           char c  = (char)i;
          
          // System.out.print("available bytes" + available);
           System.out.print(c);
         }
         fs.close();
           
        }catch(Exception e){System.out.print(e);}
       
       
    }
   
}

Program to copy bytes from input stream to output stream

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package filehandlingio;

/**
 *
 * @author shashank
 */

import java.io.*;
public class FilehandlingIO {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        FileInputStream fi = null;
        FileOutputStream fo = null;
        int i = 0;
        try
        {
            fi = new FileInputStream("C://Users//shabhatn//Documents//NetBeansProjects//FileHandlingO//fileinput.txt");
            fo = new FileOutputStream("C://Users//shabhatn//Documents//NetBeansProjects//FileHandlingO//fileoutput.txt");
           
            while((i=fi.read())!=-1)
            {
                //char c = (char)i;
                fo.write(i); //It will write byte to fileoutputfile
            }
            System.out.println("Success");
            if(fi!=null)
                    fi.close();
                if(fo!=null)
                    fo.close();
           
        }catch(Exception e){System.out.print(e);}
    }
   

}

Write a Program to read and write image.

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package filehandlingio;

/**
 *
 * @author shabhatn
 */

import java.io.*;
public class FilehandlingIO {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        FileInputStream fi = null;
        FileOutputStream fo = null;
        int i = 0;
        try
        {
            fi = new FileInputStream("D://Articles//filehandling//iostream.jpg");
            fo = new FileOutputStream("D://Articles//filehandling//ostream.jpg");
            
            while((i=fi.read())!=-1)
            {
                //char c = (char)i;
                fo.write(i); //It will write byte to fileoutputfile
            }
            System.out.println("Success");
            if(fi!=null)
                    fi.close();
                if(fo!=null)
                    fo.close();
            
        }catch(Exception e){System.out.print(e);}
    }
    
}


No comments:

Post a Comment