Java.io.FileInputStream Class in Java

19 Apr, 2022
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
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 classesimport java.io.*; // Main class// ReadFileclass 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.This article is contributed by Nishant Sharma. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
solankimayank
kashishsoda
surindertarika1234
simmytarika5
java-file-handling
Java-I/O
java-stream
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.