read(): This method is available in java.io.BufferedInputStream class of Java.
Syntax:
int java.io.BufferedInputStream.read() throws IOException
This method sees the general contract of the read method of InputStream.
Parameters: NA
Returns: the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if this input stream has been closed by invoking its close() method,or an I/O error occurs.
Approach 1: When no exception
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamread {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);System.out.println(bufferedInputStream.read());}}
Output:
-1
Approach 2: When IOException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamread {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);bufferedInputStream.close();System.out.println(bufferedInputStream.read());}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:168) at java.base/java.io.BufferedInputStream.fill(BufferedInputStream.java:212) at java.base/java.io.BufferedInputStream.read(BufferedInputStream.java:263)
No comments:
Post a Comment