Java - FilterInputStream read() method
Description
The Java FilterInputStream read() method reads data from an input stream. It reads one byte and returns it as an int (0-255). It returns -1 if the end of the stream (EOF) is reached.
Declaration
Following is the declaration for java.io.FilterInputStream.read() method −
public int read()
Parameters
NA
Return Value
The 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
Example - Usage of FilterInputStream read() method
The following example shows the usage of Java FilterInputStream read() method.
FilterInputStreamDemo.java
package com.tutorialspoint;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FilterInputStreamDemo {
public static void main(String[] args) throws Exception {
InputStream is = null;
FilterInputStream fis = null;
int i = 0;
char c;
try {
// create input streams
is = new FileInputStream("test.txt");
fis = new BufferedInputStream(is);
// read till the end of the stream
while((i = fis.read())!=-1) {
// converts integer to character
c = (char)i;
// prints
System.out.println("Character read: "+c);
}
} catch(IOException e) {
// if any I/O error occurs
e.printStackTrace();
} finally {
// releases any system resources associated with the stream
if(is!=null)
is.close();
if(fis!=null)
fis.close();
}
}
}
Output(assuming test.txt contains ABCDEF)
Let us compile and run the above program, this will produce the following result−
Character read: A Character read: B Character read: C Character read: D Character read: E Character read: F
Example - Reading One Byte at a Time Using BufferedInputStream
The following example shows the usage of Java FilterInputStream read() method.
FilterInputStreamDemo.java
package com.tutorialspoint;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
public class FilterInputStreamDemo {
public static void main(String[] args) {
try (FilterInputStream fis = new BufferedInputStream(new FileInputStream("example.txt"))) {
int data;
while ((data = fis.read()) != -1) { // Read byte by byte
System.out.print((char) data); // Convert byte to char and print
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(assuming example.txt contains Hello)
Let us compile and run the above program, this will produce the following result−
Hello
Explanation
Uses BufferedInputStream (a subclass of FilterInputStream).
Reads one byte at a time using read().
Converts byte to character ((char) data) and prints it.
Stops when read() returns -1 (EOF).
Example - Reading Multiple Bytes into a Buffer Using PushbackInputStream
The following example shows the usage of Java FilterInputStream read() method.
FilterInputStreamDemo.java
package com.tutorialspoint;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.PushbackInputStream;
public class FilterInputStreamDemo {
public static void main(String[] args) {
try (FilterInputStream fis = new PushbackInputStream(new FileInputStream("example.txt"))) {
byte[] buffer = new byte[5]; // Buffer to hold bytes
int bytesRead = fis.read(buffer, 0, buffer.length); // Read multiple bytes
System.out.println("Bytes read: " + bytesRead);
System.out.println("Data: " + new String(buffer, 0, bytesRead)); // Convert to string
} catch (IOException e) {
e.printStackTrace();
}
}
}
Output(assuming example.txt contains Microservices)
Let us compile and run the above program, this will produce the following result−
Bytes read: 5 Data: Micro
Explanation
Uses PushbackInputStream (another FilterInputStream subclass).
Reads up to 5 bytes into a buffer (byte[]).
Converts bytes to string representation and prints it.