close(): This method is available in java.io.BufferedInputStream class of Java.
Syntax:
void java.io.BufferedInputStream.close() throws IOException
This method closes this input stream and releases any system resources associated with the stream.
Once the stream has been closed, further read(), available(),reset(),or skip() invocations will throw an IOException.
Note: Closing a previously closed stream has no effect.
Parameters: NA
Returns: NA
Throws:
IOException - if 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 BufferedInputStreamclose {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);bufferedInputStream.close();System.out.println(bufferedInputStream);}}
java.io.BufferedInputStream@182decdb
Approach 2: When IOException
Java
import java.io.BufferedInputStream;import java.io.IOException;import java.io.InputStream;public class BufferedInputStreamclose {public static void main(String[] args) throws IOException {InputStream inputStream = InputStream.nullInputStream();BufferedInputStream bufferedInputStream =new BufferedInputStream(inputStream);bufferedInputStream.close();System.out.println(bufferedInputStream.available());}}
Output:
Exception in thread "main" java.io.IOException: Stream closed at java.base/java.io.BufferedInputStream.getInIfOpen(BufferedInputStream.java:157) at java.base/java.io.BufferedInputStream.available(BufferedInputStream.java:408) at com.example.BufferedInputStream.BufferedInputStreamclose.main(BufferedInputStreamclose.java:14)
No comments:
Post a Comment