GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection
  • Introduction3 min read
  • Variables2 min read
  • Data Types7 min read
  • Operators7 min read

This view

  • Original page
  • New address

GeezerVison is showing www.geeksforgeeks.org — Java FileInputStream Class - GeeksforGeeks

geeksforgeeks
search icon
  • Courses
  • Tutorials
  • Interview Prep
  • Java Tutorial
  • Advanced Java
  • Interview Questions
  • Exercises
  • Examples
  • Quizzes
  • Projects
  • Cheatsheet
  • DSA in Java
  • Java Collection

Java FileInputStream Class

Last Updated : 3 Nov, 2025

The FileInputStream class in Java is used to read data from a file in the form of bytes. It’s ideal for reading binary data such as images or audio files. For reading text files, it’s better to use FileReader.

  • Direct Access: It directly reads the file content from the disk without buffering
  • Platform Independent: It can work on any operating system

Declaration

The FileInputStream class extends the InputStream class, which means it inherits methods for reading raw byte data from files.

public class FileInputStream extends InputStream

Example: FileInputStream class to read data from file.

Java
import java.io.*;

public class Geeks{
    
    public static void main(String[] args){
        
        // Use try-with-resources to automatically close the
        // stream
        try (FileInputStream fi
             = new FileInputStream("file1.txt")) {

            // Display file channel information
            System.out.println("Channel: "
                               + fi.getChannel());

            // Display file descriptor
            System.out.println("File Descriptor: "
                               + fi.getFD());

            // Show available bytes in the stream
            System.out.println("Number of remaining bytes: "
                               + fi.available());

            // Skip first few bytes
            fi.skip(4);

            System.out.println("File Contents:");

            // Read and print file content
            int ch;
            while ((ch = fi.read()) != -1) {
                System.out.print((char)ch);
            }
        }
        catch (FileNotFoundException e) {
            System.out.println(
                "File not found: Ensure 'file1.txt' exists in the working directory.");
        }
        catch (IOException e) {
            System.out.println(
                "An error occurred while reading the file: "
                + e.getMessage());
        }
    }
}

Output: 

Screenshot
Output

Constructors of FileInputStream Class

1. FileInputStream(String name)

Creates an input file stream to read from a file with the specified name. 

FileInputStream fi = new FileInputStream("example.txt");

2. FileInputStream(File file)

Creates an input file stream to read from the specified File object. 

File f = new File("example.txt");
FileInputStream fi = new FileInputStream(f);

3. FileInputStream(FileDescriptor fdobj)

Creates an input file stream to read from the specified file descriptor. 

FileDescriptor fd = FileDescriptor.in;
FileInputStream fi = new FileInputStream(fd); 

Read Data from a File Using FileInputStream

Create a file named file.txt in your project directory with the following content:

this is my first code
this is my second code

Java
import java.io.*;

public class Geeks {

    public static void main(String[] args)
    {

         // Use try-with-resources to automatically close the stream
        try (FileInputStream fi
             = new FileInputStream("file1.txt")) {

            // Display file channel information
            System.out.println("Channel: "
                               + fi.getChannel());

            // Display file descriptor
            System.out.println("File Descriptor: "
                               + fi.getFD());

            // Illustrating available method
            System.out.println("Number of remaining bytes: "
                               + fi.available());

            // Illustrating skip() method
            fi.skip(4);

            System.out.println("File Contents:");

            // Reading characters from FileInputStream
            int ch;
            while ((ch = fi.read()) != -1) {
                System.out.print((char)ch);
            }
        }
        catch (FileNotFoundException e) {
            System.out.println(
                "File not found: Ensure 'file1.txt' exists in the working directory.");
        }
        catch (IOException e) {
            System.out.println(
                "An error occurred while reading the file: "
                + e.getMessage());
        }
    }
}

Output: 

Output
Output

Methods of Java 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
Comment

Explore

    Basics
    • Introduction3 min read
    • Variables2 min read
    • Data Types7 min read
    • Operators7 min read
    • Decision Making5 min read
    • Methods7 min read
    • Arrays7 min read
    • Strings5 min read
    OOP & Interface
    • Introduction6 min read
    • Constructors6 min read
    • Encapsulation3 min read
    • Inheritance8 min read
    • Polymorphism5 min read
    • Abstraction5 min read
    • Interfaces8 min read
    Collections
    • Collection class9 min read
    • Collection Interface5 min read
    • List Interface8 min read
    • Set Interface5 min read
    • Queue Interface5 min read
    • Map Interface in Java6 min read
    • Iterator4 min read
    Advance Java Concepts
    • Exception handling6 min read
    • Java Multithreading Tutorial3 min read
    • Synchronization in Java9 min read
    • File handling6 min read
    • Method References3 min read
    • Java 8 Stream Tutorial7 min read
    • Memory Management4 min read
    • Garbage Collection in Java6 min read
    • Networking6 min read
    • JDBC5 min read
    Practice Java
    • Interview Questions15+ min read
    • Programs6 min read
    • Practice Problems5 min read
    • Quiz1 min read
    • Project Ideas15+ min read
    Courses
    • Java Backend Course2 min read
    • Placement 360 Course2 min read
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.