Java.io.PushbackReader.close() Method



Description

The java.io.PushbackReader.close() method closes the stream and releases any system resources associated with it. Once the stream has been closed, further read(), unread(), ready(), or skip() invocations will throw an IOException.

Declaration

Following is the declaration for java.io.PushbackReader.close() method.

public void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example

The following example shows the usage of java.io.PushbackReader.close() method.

package com.tutorialspoint;

import java.io.*;

public class PushbackReaderDemo {
   public static void main(String[] args) {
      String s = "Hello World";

      // create a new StringReader
      StringReader sr = new StringReader(s);

      // create a new PushBack reader based on our string reader
      PushbackReader pr = new PushbackReader(sr, 20);
      
      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) pr.read();
            System.out.print("" + c);
         }

         // change line
         System.out.println();

         System.out.println("Closing stream...");
         // close the stream
         pr.close();
         System.out.println("Stream closed.");

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Let us compile and run the above program, this will produce the following result −

Hello
Closing stream...
Stream closed.
java_io_pushbackreader.htm
Advertisements