Java - CharArrayWriter close() method



Description

The Java CharArrayWriter close() method is used to release system resources associated with the writer. However, since CharArrayWriter does not use system I/O resources (like files or network sockets), calling close() has no effect. Unlike file-based writers (FileWriter, BufferedWriter), CharArrayWriter remains usable even after closing.

Declaration

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

public void close()

Parameters

NA

Return Value

This method does not return any value.

Exception

NA

Example - Using CharArrayWriter close() method

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

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      CharArrayWriter chw = null;
      
      try {
         // create character array writer
         chw = new CharArrayWriter();
         
         // declare character sequence
         CharSequence csq = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
         
         // closes the stream
         chw.close();
         
         // append character sequence to the writer
         chw.append(csq);
         
         // prints out the character sequences
         System.out.println(chw.toString());
         
      } catch(Exception e) {
         // for any error
         e.printStackTrace();
      } finally {
         // releases all system resources from writer
         if(chw!=null)
            chw.close();
      }
   }
}

Output

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

ABCDEFGHIJKLMNOPQRSTUVWXYZ

Example - Closing a CharArrayWriter and Writing Data After Closing

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

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;
import java.io.IOException;

public class CharArrayWriterDemo {
   public static void main(String[] args) throws IOException {
      CharArrayWriter writer = new CharArrayWriter();

      // Write data to CharArrayWriter
      writer.write("Hello, Java!");

      // Close the writer
      writer.close();

      // Append more data after closing (allowed)
      writer.append(" Still working!");

      // Convert to string and print
      System.out.println(writer.toString()); // Output: Hello, Java! Still working!
   }
}

Output

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

Hello, Java! Still working!

Explanation

  • A CharArrayWriter object is created.

  • "Hello, Java!" is written to it.

  • close() is called, but it does not affect further operations.

  • " Still working!" is appended even after closing.

  • The final output is "Hello, Java! Still working!".

Example - Using CharArrayWriter with a FileWriter After Closing

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

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;
import java.io.FileWriter;
import java.io.IOException;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      try {
         // Create CharArrayWriter
         CharArrayWriter writer = new CharArrayWriter();

         // Write data to CharArrayWriter
         writer.write("Spring Boot and Microservices");

         // Close the writer (doesn't actually disable it)
         writer.close();

         // Write contents to a file using FileWriter
         FileWriter fileWriter = new FileWriter("output.txt");
         writer.writeTo(fileWriter);

         // Close FileWriter
         fileWriter.close();

         System.out.println("Data successfully written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Data successfully written to file.

File, output.txt
Spring Boot and Microservices

Explanation

  • A CharArrayWriter object is created and data is written to it.

  • close() is called on the CharArrayWriter (which has no effect).

  • writeTo(FileWriter) transfers the content to a file (output.txt).

  • Even after closing, the data is successfully written to a file.

Key Takeaways

  • Unlike file-based writers, close() has no effect on CharArrayWriter.

  • You can still write, append, and manipulate data after closing it.

  • Useful when you want an in-memory writer that doesn't need explicit resource management.

java_io_chararraywriter.htm
Advertisements