Java.io.FileInputStream Class in Java

FileInputStream is useful to read data from a file in the form of sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader.

Constructor and Description

  • FileInputStream(File file) :Creates an input file stream to read from the specified File object.
  • FileInputStream(FileDescriptor fdobj) :Creates an input file stream to read from the specified file descriptor.
  • FileInputStream(String name) :Creates an input file stream to read from a file with the specified name.

Important Methods:

  • int read() : Reads a byte of data from this input stream.
    Syntax: public int read()
             throws IOException
    Returns: the next byte of data, or -1 if the end of the file is reached.
    Throws:
    IOException
  • int read(byte[] b) :Reads up to b.length bytes of data from this input stream into an array of bytes.
    Syntax:public int read(byte[] b)
             throws IOException
    Parameters:
    b - the buffer into which the data is read.
    Returns: the total number of bytes read into the buffer, or -1.
    Throws:
    IOException
  • int read(byte[] b, int off, int len) : Reads up to len bytes of data from this input stream into an array of bytes.
    Syntax:public int read(byte[] b,
           int off,
           int len)
             throws IOException
    Parameters:
    b - the buffer into which the data is read.
    off - the start offset in the destination array b
    len - the maximum number of bytes read.
    Returns: the total number of bytes read into the buffer, or -1 
    Throws:
    NullPointerException
    IndexOutOfBoundsException
  • long skip(long n) : Skips over and discards n bytes of data from the input stream.
    Syntax:public long skip(long n)
              throws IOException
    Parameters:
    n - the number of bytes to be skipped.
    Returns: the actual number of bytes skipped.
    Throws:
    IOException
  • int available() : Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream .
    Syntax:public int available()
                  throws IOException
    Returns: an estimate of the number of remaining bytes that can be read 
    Throws:
    IOException
  • void close() : Closes this file input stream and releases any system resources associated with the stream.
    Syntax:public void close()
               throws IOException
    Specified by:
    close in interface AutoCloseable
    Overrides:
    close in class InputStream
    Throws:
    IOException
  • FileDescriptor getFD() :Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
    Syntax :public final FileDescriptor getFD()
                               throws IOException
    Returns: the file descriptor object associated with this stream.
    Throws: IOException 
  • FileChannel getChannel() :Returns the unique FileChannel object associated with this file input stream.
    Syntax :public FileChannel getChannel()
    Returns: the file channel associated with this file input stream
  • void finalize() :Ensures that the close method of this file input stream is called when there are no more references to it.
    Syntax :protected void finalize()
                     throws IOException
    Overrides: finalize in class Object
    Throws: IOException

Steps to read data from a file using FileInputStream

  • First , attach file to a FileInputStream as shown here:
    FileInputStream  fileInputStream =new FileInputStream(“file.txt”);
  • This will enable us to read data from the file. Then , to read data from the file, we should read data from the FileInputStream as;
    ch=fileInputStream.read();

    When there is no more data available to read further , the read() method returns -1;

  • Then we should attach the monitor to output stream. For displaying the data, we can use System.out.print.
    System.out.print(ch);

Implementation:

filter_none

edit
close

play_arrow

link
brightness_4
code

import java.io.*;
//Java program demonstrating FileInputStream 
class ReadFile
{
    public static void main(String args[]) throws IOException
    {
  
        //attach the file to FileInputStream
        FileInputStream fin= new FileInputStream("file1.txt");
  
        //illustrating getChannel() method
        System.out.println(fin.getChannel());
  
        //illustrating getFD() method
        System.out.println(fin.getFD());
  
        //illustrating available method
        System.out.println("Number of remaining bytes:"+fin.available());
  
        //illustrating skip method
        /*Original File content:
        * This is my first line
        * This is my second line*/
        fin.skip(4);
        System.out.println("FileContents :");
        //read characters from FileInputStream and write them
        int ch;
        while((ch=fin.read())!=-1)
            System.out.print((char)ch);
          
        //close the file
        fin.close();
    }
}

chevron_right


Output:

sun.nio.ch.FileChannelImpl@1540e19d
java.io.FileDescriptor@677327b6
Number of remaining bytes:45
FileContents :
 is my first line
This is my second line

BufferedInputStream can be used to read a buffer full of data at a time from a file. This improves the speed of execution .

This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.



My Personal Notes arrow_drop_up


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.