Java - StringReader close() method



Description

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

Declaration

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

public abstract void close()

Parameters

NA

Return Value

This method does not return a value.

Exception

IOException − If an I/O error occurs.

Example - Usage of StringReader close() method

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

StringReaderDemo.java

package com.tutorialspoint;

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

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

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

      try {
         // read the first five chars
         for (int i = 0; i < 5; i++) {
            char c = (char) sr.read();
            System.out.print("" + c);
         }

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

         // close the stream
         sr.close();

         //try to access the reader after it is closed
         try {
            sr.ready();
            
         } catch (Exception ex) {
            // catch the exception and print a message if reader is closed
            System.out.println("Reader is closed.");
         }
         
      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

Hello
Reader is closed.

Example - Properly closing a StringReader

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

StringReaderDemo.java

package com.tutorialspoint;

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

public class StringReaderDemo {
   public static void main(String[] args) throws IOException {
      StringReader reader = new StringReader("Hello, world!");

      int data = reader.read();
      System.out.println("First character: " + (char) data);

      reader.close(); // closing the reader
      System.out.println("Reader closed.");
   }
}

Output

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

First character: H
Reader closed.

Explanation

  • The reader reads one character successfully.

  • Then it's closed using close().

  • No error occurs because we don't attempt to read after closing.

Example - Reading after closing the StringReader

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

StringReaderDemo.java

package com.tutorialspoint;

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

public class StringReaderDemo {
   public static void main(String[] args) {
      StringReader reader = new StringReader("Java");

      try {
         reader.close(); // Close the reader first

         int data = reader.read(); // Attempt to read after closing
         System.out.println("Read character: " + (char) data);
      } catch (IOException e) {
         System.out.println("Exception: " + e.getMessage());
      }
   }
}

Output

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

Exception: Stream closed

Explanation

  • Surprisingly, StringReader.read() still works even after close() (because it doesn’t hold real I/O resources).

  • This behavior is allowed but not guaranteed across all reader types, so in practice, it's always best to avoid using a stream after it's closed.

java_io_stringreader.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