(Translated by https://www.hiragana.jp/)
Java FileInputStream read() Method with Examples - GeeksforGeeks
Open In App

Java FileInputStream read() Method with Examples

Last Updated : 20 Nov, 2021
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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


Similar Reads

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.io.FileInputStream Class in Java
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 spec
3 min read
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
Reader read(char[]) method in Java with Examples
The read(char[]) method of Reader Class in Java is used to read the specified characters into an array. This method blocks the stream till: It has taken some input from the stream.Some IOException has occurredIt has reached the end of the stream while reading. Syntax: public int read(char[] charArray) Parameters: This method accepts a mandatory par
3 min read
Reader read(char[], int, int) method in Java with Examples
The read(char[], int, int) method of Reader Class in Java is used to read the specified length characters into an array at a specified offset. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(char[] charArray, int of
3 min read
Reader read(CharBuffer) method in Java with Examples
The read(CharBuffer) method of Reader Class in Java is used to read the specified characters into a CharBuffer instance. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(CharBuffer charBuffer) Parameters: This method
2 min read
Reader read() method in Java with Examples
The read() method of Reader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. This method is declared as abstract method. It means that the subclasses of Reader abstract class
3 min read
CharArrayReader read() method in Java with Examples
The read() method of CharArrayReader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. This method is declared as abstract method. It means that the subclasses of CharArrayRea
3 min read
CharArrayReader read(CharBuffer) method in Java with Examples
The read(CharBuffer) method of CharArrayReader Class in Java is used to read the specified characters into a CharBuffer instance. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(CharBuffer charBuffer) Parameters: Th
2 min read
CharArrayReader read(char[], int, int) method in Java with Examples
The read(char[], int, int) method of CharArrayReader Class in Java is used to read the specified length characters into an array at a specified offset. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(char[] charArra
3 min read
CharArrayReader read(char[]) method in Java with Examples
The read(char[]) method of CharArrayReader Class in Java is used to read the specified characters into an array. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(char[] charArray) Parameters: This method accepts a ma
2 min read
StringReader read(char[]) method in Java with Examples
The read(char[]) method of StringReader Class in Java is used to read the specified characters into an array. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(char[] charArray) Parameters: This method accepts a manda
2 min read
StringReader read(char[], int, int) method in Java with Examples
The read(char[], int, int) method of StringReader Class in Java is used to read the specified length characters into an array at a specified offset. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(char[] charArray,
3 min read
StringReader read(CharBuffer) method in Java with Examples
The read(CharBuffer) method of StringReader Class in Java is used to read the specified characters into a CharBuffer instance. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(CharBuffer charBuffer) Parameters: This
2 min read
StringReader read() method in Java with Examples
The read() method of StringReader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. This method is declared as abstract method. It means that the subclasses of StringReader ab
3 min read
PushbackReader read(char, int, int) method in Java with Examples
The read(char[], int, int) method of PushbackReader Class in Java is used to read the specified length characters into an array at a specified offset. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read(char[] charArray
3 min read
PushbackReader read() method in Java with Examples
The read() method of PushbackReader Class in Java is used to read a single character from the stream. This method blocks the stream till: It has taken some input from the stream. Some IOException has occurred It has reached the end of the stream while reading. Syntax: public int read() Parameters: This method does not accepts any parameters Return
3 min read
ObjectInputStream read() method in Java with examples
The read() method of the ObjectInputStream class in Java reads a byte of data. This method wont run if there is no data. Syntax: public int read() Parameters: This method does not accept any parameter. Return Value: This method returns the byte read, or -1 if the end of the stream is reached. Exceptions: The function throws an IOException if an I/O
1 min read
BufferedReader read() method in Java with Examples
The read() method of BufferedReader class in Java is of two types: 1. The read() method of BufferedReader class in Java is used to read a single character from the given buffered reader. This read() method reads one character at a time from the buffered stream and return it as an integer value. Syntax: public int read() throws IOException Overrides
3 min read
ByteArrayInputStream read() method in Java with Examples
The read() method of ByteArrayInputStream class in Java is used in two ways: 1. The read() method of ByteArrayInputStream class in Java is used to read the next byte of the ByteArrayInputStream. This read() method returns the byte that is read into the form of an integer and if the input stream is ended this method return -1. This method reads one
3 min read
DataInputStream read() method in Java with Examples
The read() method of DataInputStream class in Java is of two types: read(byte[] b) method of DataInputStream class in Java is used to read bytes from the input stream and store them into the buffer byte array.This read() method returns the number of bytes actually read as an integer type. This method returns -1 if the input stream is ended and no m
4 min read
PushbackInputStream read() method in Java with Examples
The read() method of PushbackInputStream class in Java is of two types: The read() method of PushbackInputStream class in Java is used to read the next byte of data from the input stream. This method returns the read byte from the input stream in the form of an integer. Syntax: public int read() throws IOException Overrides: This method overrides t
4 min read
BufferedInputStream read() method in Java with Examples
read() method of BufferedInputStream class in Java is used to read the next byte of data from the input stream. When this read() method is called on the input stream then this read() method reads one character of the input stream at a time. Syntax: public int read() Overrides: It overrides read() method of FilterInputStream class. Parameters: This
3 min read
Java FileReader Class read() Method with Examples
The read() method of FileReader class in Java is used to read and return a single character in the form of an integer value that contains the character's char value. The character read as an integer in the range of 0 to 65535 is returned by this function. If it returns -1 as an int number, it means that all of the data has been read and that FileRe
2 min read
Article Tags :
Practice Tags :
three90RightbarBannerImg