FileInputStream in Java

FileInputStream in Java is used to read binary data from the file. It is a sub class of InputStream class and available from java 1.0 version on wards.

Differenent constructors in FileOutputStream are,

  • public FileInputStream(String name) throws FileNotFoundException
  • public FileInputStream(File file) throws FileNotFoundException
  • public FileInputStream(FileDescriptior fdObj)

No param constructor is not given in the FileInputStream because it is a stream-based connection object where one end is a Java application. Therefore second end information must be passed as input.

To create a FileInputStream class object the file must exist with the passed argument name, else JVM throws java.io.FileNotFoundException. The FileInputStream class constructor throws java.io.FileNotFoundException in the given below situations,

  • If the file does not exist with the passed name.
  • The passed file name is a directory rather than it is a regular file.
  • If the file doesn’t have reading permissions.

Java FileInputStream class Methods

FileInputStream inherit all methods of InputStream class. All these methods are throws IOException.

Return typeMethodDescription
intavailable()It Returns number available bytes.
intread()Reads a byte of data from this input stream.
intread(byte b[])Reads up to b.length bytes of data from this input stream into an array of bytes.
intread(byte b[], int off, int len)It reads up to len bytes of data from this input stream into an array of bytes.
longskip(long n)Skips over and discards n bytes of data from the input stream.
voidclose()Closes this file input stream and releases any system resources associated with the stream.

Java FileInputStream Example

Steps to read binary data from a file using FileInputStream,
1) Import java.io package or related classes
2) Create a FileInputStream class object, let us say its name is fis.
3) Invoke fis.read() method to read the data
4) Invoke fis.close() method to close the stream and release the system resources
6) Handle IOException and FileNotFoundException

Properties of read() method of Java FileInputStream,

  • It read only one byte at a time in the given file.
  • It returns data in the terms of byte i.e. int value. To display a value in other types we must use typecasting.
  • If there is no byte available then it returns -1.

To read multiple bytes we must use loop until read() method doesn’t return -1.

int data;
while((data = fis.read()) != -1) {
  ...
}

We have a “data.txt” file, and we will read from this file.

data.txt

94584.4847
Hello, World!

import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
public class FileInputStreamDemo {

   public static void main(String[] args) 
     throws FileNotFoundException, IOException {

      // create object
      FileInputStream fis = new FileInputStream("data.txt");

      // read data
      int data;
      while((data = fis.read()) != -1){
         System.out.print((char)data);
      }

      // close stream
      fis.close();
   }
}

Output:-

94584.4847
Hello, World!

Instead for checking data=-1, we can also use available() method.

Limitation of FileInputStream

It read data only in a one-byte format. It is not possible to read data in the primitive or object format. To solve this problem we must use the DataInputStream class by wrapping the FileInputStream class.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *