Skip to main content

CharArrayWriter class in java

The CharArrayWriter class in Java is a subclass of Writer that allows you to write characters to an internal character array buffer. You can then retrieve the contents of the buffer as a string or char array using the toCharArray() or toString() method.

Here is an example of how to use the CharArrayWriter class:


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

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

        // Write characters to the buffer
        writer.write('T');
        writer.write('e');
        writer.write('s');
        writer.write('t');
  
        // Retrieve the contents of the buffer as a char array
        char[] charArray = writer.toCharArray();

        // Print the contents of the char array
        for (char c : charArray) {
            System.out.print(c);
        }

        // Retrieve the contents of the buffer as a string
        String str = writer.toString();
		System.out.println();
        // Print the string
        System.out.println(str);
    }
}
Test
Test

In this example, we create a CharArrayWriter instance and write the characters "Hello" to the buffer. We then retrieve the contents of the buffer as a char array and print the contents. Finally, we retrieve the contents of the buffer as a string and print the string. The output of this program would be:

public class java.io.CharArrayWriter 
extends java.io.Writer 

Here are the methods available in the CharArrayWriter class:

  1. void write(int c): Writes a single character to the buffer.
  2. void write(char[] cbuf, int off, int len): Writes a portion of an array of characters to the buffer.
  3. void write(String str, int off, int len): Writes a portion of a string to the buffer.
  4. void writeTo(Writer out): Writes the contents of the buffer to another writer.
  5. void reset(): Resets the buffer so that it is empty.
  6. char[] toCharArray(): Returns the contents of the buffer as a char array.
  7. int size(): Returns the current size of the buffer.
  8. String toString(): Returns the contents of the buffer as a string.
  9. void flush(): Flushes the buffer, but does not close the underlying stream.
  10. void close(): Closes the stream, flushing the buffer and releasing any system resources associated with it.

It's worth noting that the CharArrayWriter class extends the Writer class, so it inherits all of the methods defined in Writer. Additionally, since CharArrayWriter is a character stream class, it also inherits the methods defined in the Reader class.

This reference is belong to javadoc

Public Constructors

There is 2 types of public constructor is defined inside the class, Whose syntax as follows.

public java.io.CharArrayWriter()
public java.io.CharArrayWriter(int)

Example of CharArrayWriter() constructor.

// java program for CharArrayWriter
import java.io.CharArrayWriter;
import java.io.FileWriter;
public class Example
{
    public static void main(String[] args) throws Exception
    {
        // Create an instance
        CharArrayWriter w = new CharArrayWriter();
        w.write("I like this logic");
        // Create FileWriter instance
        FileWriter file = new FileWriter("file.txt");
        // Put value into file
        w.writeTo(file);
        file.close();
        w.close();
    }
}
Instance of java.io.CharArrayWriter class in java
File : file.txt
Contain : I like this logic




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment