Open In App

Java FileInputStream read() Method with Examples

Last Updated : 20 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 returned, or -1 if the end of the file is reached and throws an exception if an I/O error occurs. Refer to the program. 

Syntax:

public abstract int read()

Return Value: This method returns the next byte of data, or -1 if the end of the stream is reached.

Exception: IOException − If an I/O error occurs.

How to invoke the read() method?

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, 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: (“file.txt”)

GeeksforGeeks is a computer science portal

Java




// Java program to demonstrate the working
// of the FileInputStream read() method
 
import java.io.File;
import java.io.FileInputStream;
 
public class abc {
 
    public static void main(String[] args) {       
         
            // Creating file object and specifying path
            File file = new File("file.txt");
 
            try {
                FileInputStream input= new FileInputStream(file);
                int character;
                // read character by character by default
                // read() function return int between 0 and 255.
 
                while ((character = input.read()) != -1) {
                    System.out.print((char)character);
                }
            }
            catch (Exception e) {
                e.printStackTrace();
            }
    }
}


Output 

GeeksforGeeks is a computer science portal

Reading a file without using -1 in while loop

We will use the concept of the available() method in this. The available() method is used to return how many bytes are remaining to be read. We will print characters using read() method until 0 characters are left to be read.

Example: Original File content: (“file.txt”) 

GeeksforGeeks

Java




// Java program to read a file
// without using -1 in while loop
 
import java.io.File;
import java.io.FileInputStream;
 
public class abc {
 
    public static void main(String[] args) {
         
            // Creating file object and specifying path
            File file = new File("file.txt");
 
            try {
                FileInputStream input= new FileInputStream(file);
                int character;
               
                // read character by character by default
                // read() function return int between 0 and 255.
                while (input.available()!=0) {
                    character = input.read();
                    System.out.print((char)character);   
                }
               
                input.close();   
            }
       
            catch (Exception e) {
                 
                e.printStackTrace();
            }
    }
}


Output

GeeksforGeeks


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads