(Translated by https://www.hiragana.jp/)
Java.io.FileInputStream Class in Java - GeeksforGeeks
Open In App

Java.io.FileInputStream Class in Java

Last Updated : 11 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

FileInputStream class 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.

Constructors of FileInputStream Class

1. FileInputStream(File file): Creates an input file stream to read from the specified File object. 

2. FileInputStream(FileDescriptor fdobj) :Creates an input file stream to read from the specified file descriptor. 

3. FileInputStream(String name): Creates an input file stream to read from a file with the specified name. 
 

Methods of FileInputStream Class

Methods  Action Performed 
available() Returns an estimate of the number of remaining bytes that can be read (or skipped over) from this input stream.
close() Closes this file input stream and releases any system resources associated with the stream.
finalize() Ensures that the close method of this file input stream is called when there are no more references to it. 
getChannel() Returns the unique FileChannel object associated with this file input stream. 
getFD() Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.
read() Reads a byte of data from this input stream
read(byte[] b) Reads up to b.length bytes of data from this input stream into an array of bytes. 
read(byte[] b, int off, int len) Reads up to len bytes of data from this input stream into an array of bytes.
skip() Skips over and discards n bytes of data from the input stream

Now when we do use these methods we do generically follow these steps to read data from a file using FileInputStream which is ultimatum the goal of FileInputClass

Step 1: Attach a file to a FileInputStream as this will enable us to read data from the file as shown below as follows:

FileInputStream  fileInputStream =new FileInputStream(“file.txt”);

Step 2: Now in order to read data from the file, we should read data from the FileInputStream as shown below:

ch=fileInputStream.read();

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

Step 3-B: Then we should attach the monitor to the output stream. For displaying the data, we can use System.out.print.  

System.out.print(ch);

Implementation:

Original File content:

This is my first line
This is my second line

Example:

Java




// Java Program to Demonstrate FileInputStream Class
  
// Importing I/O classes
import java.io.*;
  
// Main class
// ReadFile
class GFG {
  
    // Main driver method
    public static void main(String args[])
        throws IOException
    {
  
        // Attaching 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
        fin.skip(4);
  
        // Display message for better readability
        System.out.println("FileContents :");
  
        // Reading characters from FileInputStream
        // and write them
        int ch;
  
        // Holds true till there is data inside file
        while ((ch = fin.read()) != -1)
            System.out.print((char)ch);
  
        // Close the file connections
        // using close() method
        fin.close();
    }
}


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.



Previous Article
Next Article

Similar Reads

Difference Between FileInputStream and ObjectInputStream in Java
FileInputStream class extracts input bytes from a file in a file system. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. It should be used to read byte-oriented data for example to read audio, video, images, etc. The hierarchy of classes to deal with Input s
5 min read
Difference Between FileInputStream and FileReader in Java
Let us first do discuss them in order to get the understanding alongside an example to interpret the differences. Here first we will be discussing out FileReader class. So starting of with FileReader class in java is used to read data from the file. It returns data in byte format like FileInputStream class. It is a character-oriented class that is
4 min read
FileInputStream getChannel() Method in Java with Examples
The getChannel() method is a part of Java.io.FileInputStream class. This method will return the unique FileChannel object associated with the file input stream. A channel obtained from the getChannel() method of the Java.io.FileInputStream instance will be "open for reading."getChannel() will return the FileChannel object that is associated with "t
2 min read
FileInputStream finalize() Method in Java with Examples
Java.io.FileInputStream.finalize() method is a part of Java.io.FileInputStream class. It ensures that the close method of the fileInputStream is called whenever no more references of fileInputStream exist. finalize() method is annotated @Deprecated.finalize() method is used to perform a cleanup act when no more references exist.finalize() method mi
3 min read
FileInputStream getFD() Method in Java with Examples
Java.io.FileInputStream.getFD() method is a part of Java.io.FileInputStream class. This method will return the FileDescriptor object associated with the file input stream. getFD() method is declared as final that means getFD() cannot be overridden in the subclassesFileDescriptor object that we will get using getFD() method will represent the connec
2 min read
FileInputStream skip() Method in Java with Examples
FileInputStream class is quite helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image, audio, video, etc. For reading streams of characters, FileReader is recommended. It was firstly introduced in JDK 1.0. FileInputStream skip() method The skip(n) method in FileIn
4 min read
FileInputStream close() Method in Java with Examples
FileInputStream class is helpful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. FileInputStream.close() method After any operation to the file, we have to close that file. For that purpose, we hav
2 min read
FileInputStream available() Method in Java with Examples
The available() method of FileInputStream class is used to return the estimated number of remaining bytes that can be read from the input stream without blocking. This method returns the number of bytes remaining to read from the file. When a file is completely read, this function returns zero. Syntax: FileInputStream available() Return Value: The
3 min read
Java FileInputStream read() Method with Examples
FileInputStream class in Java is useful to read data from a file in the form of a sequence of bytes. FileInputStream is meant for reading streams of raw bytes such as image data. For reading streams of characters, consider using FileReader. The read() method of InputStream class reads a byte of data from the input stream. The next byte of data is r
3 min read
Java.lang.Class class in Java | Set 1
Java provides a class with name Class in java.lang package. Instances of the class Class represent classes and interfaces in a running Java application. The primitive Java types (boolean, byte, char, short, int, long, float, and double), and the keyword void are also represented as Class objects. It has no public constructor. Class objects are cons
15+ min read
Article Tags :
Practice Tags :
three90RightbarBannerImg