Java - CharArrayWriter flush() method



Description

The Java CharArrayWriter flush() method is used to clear any buffered data and ensure that it is written out. However, since CharArrayWriter does not use an actual buffer, calling flush() has no effectunlike file-based writers like FileWriter or BufferedWriter, which require flushing to ensure data is written to the underlying stream.

Declaration

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

public void flush()

Parameters

NA

Return Value

This method does not return any value.

Exception

NA

Example - Using CharArrayWriter flush() method

The following example shows the usage of Java CharArrayWriter flush() 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";
         
         // append character sequence to the writer
         chw.append(csq);
         
         // flush
         chw.flush();
         
         // 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 - Calling flush() Before Writing to a File

The following example shows the usage of Java CharArrayWriter flush() 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
         writer.write("Spring Boot is awesome!");

         // Flush the writer (has no real effect in CharArrayWriter)
         writer.flush();

         // Write contents to a file
         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 (output.txt)

Spring Boot is awesome!

Explanation

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

  • flush() is called, but it does nothing since CharArrayWriter does not buffer data.

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

  • The file is successfully written without requiring flush().

Example - Using flush() with append() and Checking Output

The following example shows the usage of Java CharArrayWriter flush() 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 initial data
      writer.write("Hello");

      // Flush the writer (does nothing)
      writer.flush();

      // Append more data
      writer.append(", Java!");

      // Print the output
      System.out.println(writer.toString()); // Output: Hello, Java!
   }
}

Output

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

Hello, Java!

Explanation

  • A CharArrayWriter instance is created and "Hello" is written.

  • flush() is called, but it does nothing.

  • append(", Java!") adds more content.

  • The final output is "Hello, Java!", proving that flush() had no effect.

Key Takeaways

  • Unlike BufferedWriter, flush() does not affect CharArrayWriter.

  • It does not clear or reset the writerdata remains unchanged.

  • Useful when writing to external streams (writeTo()), but not needed otherwise.

java_io_chararraywriter.htm
Advertisements