Java - FileInputStream read() method



Description

The Java FileInputStream read() method reads a byte of data from this input stream. The method blocks if no input is available.

Declaration

Following is the declaration for java.io.FileInputStream.read() method −

public int read()

Parameters

NA

Return Value

The methods returns the next byte of data, or -1 if the end of the file is reached.

Exception

IOException− If any I/O error occurs.

Example - Usage of FileInputStream read() method

The following example shows the usage of Java FileInputStream read() method.

FileInputStreamDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.FileInputStream;

public class FileInputStreamDemo {
   public static void main(String[] args) throws IOException {
      FileInputStream fis = null;
      int i = 0;
      char c;
      
      try {
         // create new file input stream
         fis = new FileInputStream("test.txt");
         
         // read till the end of the file
         while((i = fis.read())!=-1) {
         
            // converts integer to character
            c = (char)i;
            
            // prints character
            System.out.print(c);
         }
         
      } catch(Exception ex) {
         // if any error occurs
         ex.printStackTrace();
      } finally {
         // releases all system resources from the streams
         if(fis!=null)
            fis.close();
      }
   }
}

Output

Assumption

Assuming we have a text file test.txt in current directory, which has the following content. This file will be used as an input for our example program.

ABCDEF

Let us compile and run the above program, this will produce the following result−

ABCDEF

Example - Using read() to Read a Single Byte

The following example shows the usage of Java FileInputStream read() method.

FileInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Open a file input stream
         FileInputStream fis = new FileInputStream("example.txt");

         // Read and print each byte until the end of the file (-1)
         int byteData;
         while ((byteData = fis.read()) != -1) {
            // Convert byte to character and print
            System.out.print((char) byteData);
         }

         // Close the stream
         fis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(contents of example.txt)

Let us compile and run the above program, this will produce the following result−

Hello, World!

Explanation

  • A FileInputStream is created for "example.txt".

  • The read() method reads one byte at a time and returns an integer.

  • The loop continues reading until read() returns -1, indicating the end of the file.

  • Each byte is cast to a character before printing.

  • The stream is closed to release resources.

Example - Using read(byte[] b) to Read Multiple Bytes

The following example shows the usage of Java FileInputStream read(byte[] b) method.

FileInputStreamDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.IOException;

public class FileInputStreamDemo {
   public static void main(String[] args) {
      try {
         // Open a file input stream
         FileInputStream fis = new FileInputStream("example.txt");

         // Create a byte array buffer
         byte[] buffer = new byte[10]; // Read 10 bytes at a time
         int bytesRead;

         // Read chunks of bytes into the buffer
         while ((bytesRead = fis.read(buffer)) != -1) {
            // Convert bytes to string and print
            System.out.print(new String(buffer, 0, bytesRead));
         }

         // Close the stream
         fis.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output(contents of example.txt)

Let us compile and run the above program, this will produce the following result−

Hello, World!

Explanation

  • A FileInputStream is created for "example.txt".

  • A byte array buffer (buffer[10]) is used to read data in chunks of 10 bytes.

  • The read(byte[] b) method fills the buffer and returns the number of bytes read.

  • A loop continues reading until read() returns -1, meaning the end of the file is reached.

  • The bytes read are converted into a String and printed.

  • The stream is closed after reading.

java_io_fileinputstream.htm
Advertisements