in , ,

Java FileInputStream Class

Java FileInputStream Class
Java FileInputStream Class

In this tutorial, we will learn about Java FileInputStream and its strategies with the help of examples.

The FileInputStream class of the java.io package can be used to understand data (in bytes) from files.

It expands the InputStream abstract class.

Before we learn about FileInputStream, make sure to know about Java Files.


Make a FileInputStream

To make a record input stream, we should import the java.io.FileInputStream package first. When we import the package, here is the means by which we can make a file input stream in Java.

1. Using the path to file

FileInputStream input = new FileInputStream(stringPath);

Here, we have created an input stream that will be linked to the file specified by the path.

  1. Using an object of the file
FileInputStream input = new FileInputStream(File fileObject);

Here, we have created an input stream that will be linked to the file specified by fileObject.


Methods of FileInputStream

The FileInputStream class provides implementations for different methods present in the InputStream class.

read() Method

  • read() – reads a single byte from the file
  • read(byte[] array) – reads the bytes from the file and stores in the specified array
  • read(byte[] array, int start, int length) – reads the number of bytes equal to length from the file and stores in the specified array starting from the position start

Suppose we have a file named input.txt with the following content.

This is a line of text inside the file.

Let’s try to read this file using FileInputStream.

import java.io.FileInputStream;

public class Main {

  public static void main(String args[]) {

     try {
        FileInputStream input = new FileInputStream("input.txt");

        System.out.println("Data in the file: ");

        // Reads the first byte
        int i = input.read();

       while(i != -1) {
           System.out.print((char)i);

           // Reads next byte from the file
           i = input.read();
        }
        input.close();
     }

     catch(Exception e) {
        e.getStackTrace();
     }
  }
}

Output

Data in the file:
This is a line of text inside the file.

In the above example, we have made a file input stream named input. The input stream is connected with the input.txt file.

FileInputStream input = new FileInputStream("input.txt");

To read data from the file, we have used the read() method inside the while loop.


available() Method

To get the number of available bytes, we can use the available() method. For example,

import java.io.FileInputStream;

public class Main {

   public static void main(String args[]) {

      try {
         // Suppose, the input.txt file contains the following text
         // This is a line of text inside the file.
         FileInputStream input = new FileInputStream("input.txt");

         // Returns the number of available bytes
         System.out.println("Available bytes at the beginning: " + input.available());

         // Reads 3 bytes from the file
         input.read();
         input.read();
         input.read();

         // Returns the number of available bytes
         System.out.println("Available bytes at the end: " + input.available());

         input.close();
      }

      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

Output

Available bytes at the beginning: 39
Available bytes at the end: 36

In the above example,

  1. We first use the available() method to check the number of available bytes in the file input stream.

2. We at that point have used the read() technique 3 times to peruse 3 bytes from the record input stream.

3. Presently, subsequent to perusing the bytes we again have checked the accessible bytes. This time the available bytes diminished by 3.


skip() Method

To discard and skip the specified number of bytes, we can use the skip() method. For example,

import java.io.FileInputStream;

public class Main {

   public static void main(String args[]) {

      try {
         // Suppose, the input.txt file contains the following text
         // This is a line of text inside the file.
         FileInputStream input = new FileInputStream("input.txt");

         // Skips the 5 bytes
         input.skip(5);
         System.out.println("Input stream after skipping 5 bytes:");

         // Reads the first byte
         int i = input.read();
         while (i != -1) {
            System.out.print((char) i);

            // Reads next byte from the file
            i = input.read();
         }

         // close() method
         input.close();
      }
      catch (Exception e) {
         e.getStackTrace();
      }
   }
}

Output

Input Stream after skipping 5 bytes:
is a line of text inside the file.

In the above example, we have used the skip() strategy to avoid 5 bytes of data from the record input stream. Consequently, the bytes representing the content “This ” isn’t read from the input stream.


close() Method

To close the file input stream, we can use the close() technique. When the close() strategy is called, we can’t use the input stream to understand data.

In all the above examples, we have used the close() strategy to close the file input stream.


Other Methods Of FileInputStream

MethodsDescriptions
finalize()ensures that the close() method is called
getChannel()returns the object of FileChannel associated with the input stream
getFD()returns the file descriptor associated with the input stream
mark()mark the position in input stream up to which data has been read
reset()returns the control to the point in the input stream where the mark was set

To learn more, visit Java FileInputStream (official Java documentation).


Thanks for reading! We hope you found this tutorial helpful and we would love to hear your feedback in the Comments section below. And show us what you’ve learned by sharing your photos and creative projects with us.

salman khan

Written by worldofitech

Leave a Reply

Java OutputStream Class

Java OutputStream Class

Java FileOutputStream Class

Java FileOutputStream Class