Java - PushbackReader close() method



Description

The Java 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.

close() method −

  • Closes the stream and releases any system resources associated with it.

  • Once closed, further read() or unread() operations 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 - Usage of PushbackReader close() method

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

PushbackReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;

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();
      }
   }
}

Output

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

Hello
Closing stream...
Stream closed.

Example - Properly closing a PushbackReader

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

PushbackReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;

public class PushbackReaderDemo {
   public static void main(String[] args) {
      try (PushbackReader reader = new PushbackReader(new StringReader("Hello, Java!"))) {
         int ch = reader.read();
         System.out.println("First character read: " + (char) ch); // Output: H

         // Push the character back
         reader.unread(ch);

         // Read it again
         System.out.println("After unread: " + (char) reader.read()); // Output: H

         // The reader will be automatically closed here due to try-with-resources
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

First character read: H
After unread: H

Explanation

  • Uses try-with-resources to ensure close() is automatically called at the end.

  • Proper resource handling without explicitly calling close().

Example - Calling close() explicitly and catching error after close

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

PushbackReaderDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PushbackReader;
import java.io.StringReader;

public class PushbackReaderDemo {
   public static void main(String[] args) {
      PushbackReader reader = null;

      try {
         reader = new PushbackReader(new StringReader("ABC"));

         int ch1 = reader.read();
         System.out.println("Read: " + (char) ch1); // Output: A

         reader.close(); // Close the stream explicitly

         // Attempting to read after closing should fail
         int ch2 = reader.read(); // This will throw IOException
         System.out.println("This won't print: " + (char) ch2);

      } catch (IOException e) {
         System.out.println("IOException occurred: " + e.getMessage());
      } finally {
         try {
            if (reader != null) reader.close(); // Safe close if not already closed
         } catch (IOException ignored) {}
      }
   }
}

Output

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

Read: A
IOException occurred: Stream closed

Explanation

  • Demonstrates explicit close() followed by an invalid read operation.

  • Shows that reading from a closed PushbackReader results in an IOException.

java_io_pushbackreader.htm