PushbackInputStream close() method in Java with Examples
Last Updated :
05 Jun, 2020
The close() method of PushbackInputStream class in Java is used to closes the input stream and it releases resources of system that are associated with the stream. After calling this method, further calling other method if this class will throw the IOException.
Syntax:
public void close()
throws IOException
Specified By: This method is specified by the close() method AutoCloseable interface and the close() method of Closeable interface.
Overrides: This method overrides the close() method of FilterInputStream class.
Parameters: This method does not accept any parameter.
Return value: This method does not return any value.
Exceptions: This method throws IOException if an I/O error occurs.
Below programs illustrate close() method of PushbackInputStream class in IO package:
Program 1:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
try {
byte [] byteArray
= new byte [] { 'G' , 'E' , 'E' ,
'K' , 'S' };
InputStream inputStr
= new ByteArrayInputStream(byteArray);
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
for ( int i = 0 ; i < byteArray.length; i++) {
System.out.print(
( char )pushbackInputStr.read());
}
pushbackInputStr.close();
}
catch (Exception e) {
System.out.println( "Stream is closed" );
}
}
}
|
Program 2:
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
try {
byte [] byteArray
= new byte [] { 'G' , 'E' , 'E' ,
'K' , 'S' };
InputStream inputStr
= new ByteArrayInputStream(byteArray);
PushbackInputStream pushbackInputStr
= new PushbackInputStream(inputStr);
pushbackInputStr.close();
for ( int i = 0 ; i < byteArray.length; i++) {
System.out.print(
( char )pushbackInputStr.read());
}
}
catch (Exception e) {
System.out.println( "Stream is closed" );
}
}
}
|
References:
https://docs.oracle.com/javase/10/docs/api/java/io/PushbackInputStream.html#close()
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...