Java-FileInputStream

FileInputStream

FileInputStream class possesses the functionality of reading one byte at a time from a file. In the following program, an object is created for the FileInputStream class by passing the data file data.da as the argument for its constructor. Since the bytes have been written into this data file in the previous program, they are retrieved, one by one, and then displayed on the screen. The read( ) method in the FileInputStream class reads a single byte at a time. Finally the file ‘data.da’ is closed.

Program

hgh

Program Source

import java.io.FileInputStream;
import java.io.IOException;

class Javaapp {
    
    public static void main(String[] args) {
        
        FileInputStream fi = null;
        
        try{
            fi = new FileInputStream("data.da");
            int getSize = fi.available();
            System.out.println("File Size in Bytes : "+getSize);
            
            int i = 0;
            while(i<getSize)
            {
                System.out.println(fi.read());
                i++;
            }
        }catch(IOException io)
        {
            System.out.println("Exception : "+io);
            
        }finally{
            
            try{
                fi.close();
                
            }catch(IOException io)
            {
                System.out.println("Exception : "+io);
            }
        }
    }
}

Leave a Comment