The Wayback Machine - https://web.archive.org/web/20241003000328/https://www.geeksforgeeks.org/java-io-fileinputstream-class-java/
Open In App

Java.io.FileInputStream Class in Java

Last Updated : 11 Sep, 2023
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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
Java.lang.Class class in Java | Set 2
Java.lang.Class class in Java | Set 1 More methods: 1. int getModifiers() : This method returns the Java language modifiers for this class or interface, encoded in an integer. The modifiers consist of the Java Virtual Machine's constants for public, protected, private, final, static, abstract and interface. These modifiers are already decoded in Mo
15+ min read
Java.util.TimeZone Class (Set-2) | Example On TimeZone Class
TimeZone class (the methods of this class was discussed in this article Java.util.TimeZone Class | Set 1) can be used in many cases like using TimeZone class we can get the time difference in term of hour and minute between two places.Problem : How we can get time difference of time in terms of hours and minutes between two places of Earth?Solution
5 min read
Implement Pair Class with Unit Class in Java using JavaTuples
To implement a Pair class with a Unit class in Java using JavaTuples, you can use the Pair class provided by the library and create a new Unit class that extends the Unit class provided by the library. Here is an example implementation: Here is an example Java code that uses the MyPair class with MyUnit and displays the output: Java Code import org
4 min read
Implement Triplet Class with Pair Class in Java using JavaTuples
Following are the ways to implement Triplet Class with Pair Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Pair Pair<Integer, String> pair = new Pair<Integer, String>( Integer.valueOf(1), "GeeksforGeeks"); // Print the Pair System.out.printl
2 min read
Implement Quintet Class with Quartet Class in Java using JavaTuples
Following are the ways to implement Quintet Class with Quartet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Quartet Quartet<String, String, String, String> quartet = new Quartet<String, String, String, String>( "Quartet", "Triplet
4 min read
Implement Quartet Class with Triplet Class in Java using JavaTuples
Following are the ways to implement Quartet Class with Triplet Class Using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Triplet Triplet<String, String, String> triplet = new Triplet<String, String, String>( "Triplet 1", "1", "GeeksforGe
3 min read
Implement Octet Class from Septet Class in Java using JavaTuples
Prerequisite: Octet Class, Septet Class Below are the methods to implement a Octet Class using Septet Class in Java: Using direct values // Java program to illustrate // implementing Octet Class // from Septet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Sep
6 min read
Implement Ennead Class from Octet Class in Java using JavaTuples
Prerequisite: Ennead Class, Octet Class Below are the methods to implement a Ennead Class using Octet Class in Java: Using direct values // Java program to illustrate // implementing Ennead Class // from Octet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create Oct
7 min read
Implement Sextet Class from Quintet Class in Java using JavaTuples
Prerequisite: Sextet Class, Quintet Class Below are the methods to implement a Sextet Class using Quintet Class in Java: Using direct values // Java program to illustrate // implementing Sextet Class // from Quintet Class using // direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // crea
5 min read
Implement Septet Class from Sextet Class in Java using JavaTuples
Prerequisite: Septet Class, Sextet Class Below are the methods to implement a Septet Class using Sextet Class in Java: Using direct values // Java program to illustrate // implementing Septet Class // from Sextet Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
6 min read
Implement Decade Class from Ennead Class in Java using JavaTuples
Prerequisite: Decade Class, Ennead Class Below are the methods to implement a Decade Class using Ennead Class in Java: Using direct values // Java program to illustrate // implementing Decade Class // from Ennead Class // using direct values import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // Create
8 min read
Difference between Abstract Class and Concrete Class in Java
Abstract Class: An abstract class is a type of class in Java that is declared by the abstract keyword. An abstract class cannot be instantiated directly, i.e. the object of such class cannot be created directly using the new keyword. An abstract class can be instantiated either by a concrete subclass or by defining all the abstract method along wit
5 min read
Java - Inner Class vs Sub Class
Inner Class In Java, one can define a new class inside any other class. Such classes are known as Inner class. It is a non-static class, hence, it cannot define any static members in itself. Every instance has access to instance members of containing class. It is of three types: Nested Inner ClassMethod Local Inner ClassAnonymous Inner Class Genera
3 min read
Java I/O Operation - Wrapper Class vs Primitive Class Variables
It is better to use the Primitive Class variable for the I/O operation unless there is a necessity of using the Wrapper Class. In this article, we can discuss briefly both wrapper class and primitive data type. A primitive data type focuses on variable values, without any additional methods.The Default value of Primitive class variables are given b
3 min read
Using predefined class name as Class or Variable name in Java
In Java, you can use any valid identifier as a class or variable name. However, it is not recommended to use a predefined class name as a class or variable name in Java. The reason is that when you use a predefined class name as a class or variable name, you can potentially create confusion and make your code harder to read and understand. It may a
5 min read
Why BufferedReader class takes less time for I/O operation than Scanner class in java
In Java, both BufferReader and Scanner classes can be used for the reading inputs, but they differ in the performance due to their underlying implementations. The BufferReader class can be generally faster than the Scanner class because of the way it handles the input and parsing. This article will guide these difference, provide the detailed expla
3 min read
Does JVM create object of Main class (the class with main())?
Consider following program. class Main { public static void main(String args[]) { System.out.println("Hello"); } } Output: Hello Does JVM create an object of class Main? The answer is "No". We have studied that the reason for main() static in Java is to make sure that the main() can be called without any instance. To justify the same, we
1 min read
Java.io.ObjectInputStream Class in Java | Set 2
Java.io.ObjectInputStream Class in Java | Set 1 Note : Java codes mentioned in this article won't run on Online IDE as the file used in the code doesn't exists online. So, to verify the working of the codes, you can copy them to your System and can run it over there. More Methods of ObjectInputStream Class : defaultReadObject() : java.io.ObjectInpu
6 min read
Java.lang.StrictMath class in Java | Set 2
Java.lang.StrictMath Class in Java | Set 1More methods of java.lang.StrictMath class 13. exp() : java.lang.StrictMath.exp(double arg) method returns the Euler’s number raised to the power of double argument. Important cases: Result is NaN, if argument is NaN.Result is +ve infinity, if the argument is +ve infinity.Result is +ve zero, if argument is
6 min read
java.lang.instrument.ClassDefinition Class in Java
This class is used to bind together the supplied class and class file bytes in a single ClassDefinition object. These class provide methods to extract information about the type of class and class file bytes of an object. This class is a subclass of java.lang.Object class. Class declaration: public final class ClassDefinition extends ObjectConstruc
2 min read
Article Tags :
Practice Tags :