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.


FileInputStream Constructors

Constructors Description
FileInputStream(String name) Creates a FileInputStream by opening a connection to an actual file, the file named by the path name name in the file system.
FileInputStream(File file) Creates a FileInputStream by opening a connection to an actual file, the file named by the File object file in the file system.
FileInputStream(FileDescriptor fdObj) Creates a FileInputStream by using the file descriptor fdObj, which represents an existing connection to an actual file in the file system.



FileInputStream Methods

Method Description
int read() Reads a byte of data from this input stream.
int read(byte[] b) Reads up to b.length bytes of data from this input stream into an array of bytes.
int read(byte[] b, int off, int len) Reads up to len bytes of data from this input stream into an array of bytes.
FileChannel getChannel() Returns the file channel object associated with this file output stream.
FileDescriptor getFD() Returns the file descriptor associated with this stream.
int available() Returns the number of remaining bytes that can be read from the input stream.
boolean markSupported() Tells whether this stream supports the mark() operation, which it does.
void mark(int readAheadLimit) Sets the current marked position in the stream. The parameter gives the maximum limit of bytes that can be read before the marked position becomes invalid.
void reset() Resets the stream to the most recent mark.
long skip(long n) Skips over and discards n bytes of data from the input stream.
void close() Closes the stream and releases any system resources associated with it.


Program

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