FilterInputStream read() in Java

read(): This method is available in the java.io.FilterInputStream class of Java.

Syntax:

int java.io.FilterInputStream.read() throws IOException

This method reads the next byte of data from this input stream.

The value byte is returned as an int in the range 0 to 255.

If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.

Parameters: NA

Returns: the next byte of data, or -1 if the end of the stream is reached.

Throws:

IOException - if an I/O error occurs.

Approach

Java

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FilterInputStream;
import java.io.IOException;

public class FilterInputStreamread {
    public static void main(String[] args) throws IOException {

        File data = new File("D:\\hello.txt");
        FileInputStream file = new FileInputStream(data);

        FilterInputStream filter =
new BufferedInputStream(file);

        System.out.println(filter.read());

        filter.close();
    }
}

Output:

-1


Other methods of FilterInputStream

available(): This method returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking the next caller of a method for this input stream.

close(): This method closes this input stream and releases any system resources associated with the stream.

mark(int): This method marks the current position in this input stream.

markSupported(): This method tests if this input stream supports the mark and reset methods.

read(byte[]): This method reads up to b.length bytes of data from this input stream into an array of bytes.

read(byte[], int, int): This method reads up to len bytes of data from this input stream into an array of bytes.

reset(): This method repositions this stream to the position at the time the mark method was last called on this input stream.

skip(long): This method skips over and discards n bytes of data from the input stream.


No comments:

Post a Comment