Java FileInputStream read() Method with Examples
Last Updated :
20 Nov, 2021
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 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. FileInpu
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, w
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. Synt
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
2 min read
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 retur
2 min read
Java FileReader Class ready() Method with Examples Java FileReader class is used to read data from the data file. It returns the data in byte format as FileInputStream class. This is a character-oriented class that is used for file handling in Java. ready() Method of FileReader Class It checks if the file reader is ready to be read. It will return a
3 min read
Reader mark(int) method in Java with Examples The mark() method of Reader Class in Java is used to mark the stream as the checkpoint from where the stream read will start, once reset() is called. This method is not supported by all subclasses of Reader class. Syntax: public void mark(int readAheadLimit) Parameters: This method accepts a mandato
3 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
3 min read
Reader ready() method in Java with Examples The ready() method of Reader Class in Java is used to check whether this Reader is ready to be read or not. It returns a boolean which states if the reader is ready. Syntax: public void ready() Parameters: This method does not accepts any parameters Return Value: This method returns a boolean value
3 min read
Reader close() method in Java with Examples The close() method of Reader Class in Java is used to close the stream and release the resources that were busy in the stream, if any. This method has following results: If the stream is open, it closes the stream releasing the resources If the stream is already closed, it will have no effect. If an
2 min read