Java - PushbackInputStream close() method



Description

The Java PushbackInputStream close() method closes this input stream and releases any system resources associated with the stream. Once the stream has been closed, further read(), unread(), available(), reset(), or skip() invocations will throw an IOException. Closing a previously closed stream has no effect.

The close() method

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

  • After closing, further read(), unread(), or available() calls will throw an IOException.

  • This also closes the underlying input stream.

Declaration

Following is the declaration for java.io.PushbackInputStream.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 PushbackInputStream close() method

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

PushbackInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.PushbackInputStream;

public class PushbackInputStreamDemo {
   public static void main(String[] args) {
   
      // declare a buffer and initialize its size:
      byte[] arrByte = new byte[1024];

      // create an array for our message
      byte[] byteArray = new byte[]{'H', 'e', 'l', 'l', 'o'};

      // create object of PushbackInputStream class for specified stream
      InputStream is = new ByteArrayInputStream(byteArray);
      PushbackInputStream pis = new PushbackInputStream(is);

      try {
         // read from the buffer one character at a time
         for (int i = 0; i < byteArray.length; i++) {

            // read a char into our array
            arrByte[i] = (byte) pis.read();

            // display the read byte
            System.out.print((char) arrByte[i]);
         }

         System.out.println("\nClosing the stream...");

         // close the stream
         pis.close();

         System.out.println("Stream closed.");
         
      } catch (Exception ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello
Closing the stream...
Stream closed.

Example - Close a stream after reading

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

PushbackInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.PushbackInputStream;
import java.io.IOException;

public class PushbackInputStreamDemo {
   public static void main(String[] args) {
      byte[] data = "Java".getBytes();

      try (PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data))) {
         int ch;
         while ((ch = pbis.read()) != -1) {
            System.out.print((char) ch); // Output: Java
         }
         // Stream will be closed automatically by try-with-resources
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Java

Explanation

  • A PushbackInputStream is created over a ByteArrayInputStream.

  • It reads and prints all characters.

  • The stream is closed automatically using try-with-resources.

Example - Manually closing a stream and attempting to read

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

PushbackInputStreamDemo.java

package com.tutorialspoint;

import java.io.ByteArrayInputStream;
import java.io.PushbackInputStream;
import java.io.IOException;

public class PushbackInputStreamDemo {
   public static void main(String[] args) {
      byte[] data = "Stream".getBytes();

      PushbackInputStream pbis = new PushbackInputStream(new ByteArrayInputStream(data));

      try {
         int ch = pbis.read();  // Read 'S'
         System.out.println("Read: " + (char) ch); // Output: S

         pbis.close(); // Manually close the stream

         pbis.read(); // This will throw IOException
      } catch (IOException e) {
         System.out.println("Exception after close: " + e); // Expected IOException
      }
   }
}

Output

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

Read: S
Exception after close: java.io.IOException: Stream closed

Explanation

  • We read the first character 'S'.

  • After calling close(), any further operation (like read()) throws an IOException, demonstrating that the stream is no longer usable.

java_io_pushbackinputstream.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy