Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Java Arrays
  • Java Strings
  • Java OOPs
  • Java Collection
  • Java 8 Tutorial
  • Java Multithreading
  • Java Exception Handling
  • Java Programs
  • Java Project
  • Java Collections Interview
  • Java Interview Questions
  • Java MCQs
  • Spring
  • Spring MVC
  • Spring Boot
  • Hibernate
Open In App
Next Article:
CharArrayWriter append() method in Java with Examples
Next article icon

Java CharArrayWriter Class | Set 1

Last Updated : 18 Apr, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

The CharArrayWriter class in Java is part of the java.io package, and it is used to write data into a character array. This class is used when we want to store characters temporarily without dealing with files and other storage.

Features of CharArrayWriter Class:,

  • It is used to store characters in memory, not in files.
  • The internal buffer increases automatically as more characters are written to it.
  • We can also convert the stored characters into a String[] or a char[] array.
  • It is useful when we want to store the characters temporarily in memory.

Example:

Java
// Demonstrating the working of CharArrayWriter import java.io.CharArrayWriter;  public class Geeks {     public static void main(String[] args) {                  // Create a CharArrayWriter         CharArrayWriter w = new CharArrayWriter();                  // Write some characters         w.write('G');         w.write('e');         w.write('e');         w.write('k');         w.write('s');                  // Convert it to a string and print it         String s = w.toString();         System.out.println(s);                  // Close the writer         w.close();     } } 

Output
Geeks 


Declaration of CharArrayWriter Class

The Declaration of CharArrayWriter class is listed below:

public class CharArrayWriter extends Writer

All Implemented Interfaces:

  • Closeable: This interface make sure that all the resources are properly released when we are done using the CharArrayWriter.
  • Flushable: This inteface flush the content and making sure any stored data is written out.
  • Appendable: This interface lets us add characters to the writer.
  • AutoCloseable: This interface enables automatic resource management.

Constructors in CharArrayWriter in Java

This class consists of two constructors with the help of which we can create object of this class in different ways. The following are the constructors available in this class:

1. CharArrayWriter(): This constructor creates a new CharArrayWriter with an initial buffer size of 32 characters.

Syntax:

CharArrayWriter charArrayWriter = new CharArrayWriter();

Example:

Java
// Demonstrating the working of  // CharArrayWriter() import java.io.CharArrayWriter;  public class Geeks {     public static void main(String[] args) {                  // Create a CharArrayWriter with default size         CharArrayWriter w = new CharArrayWriter();                  // Write some characters         w.write('O');         w.write('n');         w.write('e');                  // Convert it to a string and print it         String s = w.toString();         System.out.println(s);                  // Close the writer         w.close();     } } 

Output
One 


2. CharArrayWriter(int size): This constructor creates a new CharArrayWriter with the specified initial buffer size.

Syntax:

CharArrayWriter charArrayWriter = new CharArrayWriter(64);

Example:

Java
// Demonstrating the working  // of CharArrayWriter(int size) import java.io.CharArrayWriter;  public class Geeks {     public static void main(String[] args) {                  // Create a CharArrayWriter with a specified initial size         CharArrayWriter w = new CharArrayWriter(50);  // Size is 50                  // Write some characters         w.write('J');         w.write('a');         w.write('v');         w.write('a');                  // Convert it to a string and print it         String s = w.toString();         System.out.println(s);                  // Close the writer         w.close();     } } 

Output
Java 


Java CharArrayWriter Methods

The image below demonstrates the methods of CharArrayWriter class.

CharArrayWriter class in Java - Set 1


Now, we are going to discuss about each method one by one in detail:

1. write(int char): This method is used to writes a single character to the writer.

Syntax:

public void write(int char)

Parameters: The integer value of the character to be written.


2. write(String str, int offset, int maxlen): This method is used to writes a part of a string to the writer.

Syntax:

public void write(String str, int offset, int maxlen)

Parameters: This method includes three parameters which are listed below

  • str: The string to be written to the Writer.
  • offset: It is the starting position of the string.
  • maxlen: Maximum number of characters to write from the string.


3. write(char[] carray, int offset, int maxlen): This method is used to writes a part of a character array to the Writer. We can tell where to start in the array and how many characters to write.

Syntax:

public void write(char[] carray, int offset, int maxlen)

Parameters: This method includes three parameters which are listed below

  • carray: The character array to be written to the Writer.
  • offset: It is the starting position in the character array
  • maxlen: Maximum number of characters to write from the character array.


4. writeTo(Writer out_stream): This method is used to Writes the content of the buffer to another specified stream.

Syntax:

public void writeTo(Writer out_stream)

Parameters: The destination Writer stream to which the buffer content will be written.


5. toString(): This method is used to convert the buffer content into a string representation.

Syntax:

public String toString()

  • Parameters: This method does not take any parameter
  • Return Type: This method returns the buffer content as a string


6. close(): This method closes the writer stream but does not release the buffer.

  • Parameters: This method does not take any parameter.


7. size(): This method returns the size of a buffer.

  • Parameters: This method does not take any parameter.


Java Program to Demonstrate Methods of CharArrayWriter Class

Example:

Java
// Java program illustrating the working of CharArrayWriter class methods // write(int char), toString(), write(char[] carray, int offset, int maxlen) // write(String str, int offset, int maxlen), size() import java.io.*;  public class Geeks {          public static void main(String[] args) throws IOException     {         // Initializing the character array         char[] geek = {'G', 'E', 'E', 'K', 'S'};         String geek_str;          // Initializing the CharArrayWriter         CharArrayWriter char_array1 = new CharArrayWriter();         CharArrayWriter char_array2 = new CharArrayWriter();         CharArrayWriter char_array3 = new CharArrayWriter();          for(int c = 72; c < 77; c++)         {             // Use of write(int char)             // Writer int value to the Writer             char_array1.write(c);         }          // Use of toString() : returning Buffer content as String         geek_str = char_array1.toString();         System.out.println("Using write(int char) : "+ geek_str);           // Use of write(String str, int offset, int maxlen)         // writes some part of the string to the Writer.         char_array2.write(geek_str, 2, 3);          System.out.println("write(str, offset, maxlen) : "+ char_array2.toString());           // Use of write(char[] carray, int offset, int maxlen)         // writes some part of the Char[] geek to the Writer         char_array3.write(geek, 2, 3);         System.out.println("write(carray, offset, maxlen) : "+ char_array3.toString());          // get buffered content as string         String str = char_array3.toString();           // Use of writeTo(Writer out_stream)         char_array3.writeTo(char_array1);          System.out.println("\nchar_array3 to char_array1 : "+ char_array1.toString());           // Use of size() method         System.out.println("\nSize of char_array1 : "+ char_array1.size());         System.out.println("Size of char_array1 : "+ char_array2.size());         System.out.println("Size of char_array1 : "+ char_array3.size());      } } 


Output:

Using write(int char) : HIJKL
write(str, offset, maxlen) : JKL
write(carray, offset, maxlen) : EKS
char_array3 to char_array1 : HIJKLEKS
Size of char_array1 : 8
Size of char_array1 : 3
Size of char_array1 : 3


To know about other methods, refer this article: Java.io.CharArrayWriter class in Java | Set2



Next Article
CharArrayWriter append() method in Java with Examples

M

Mohit Gupta
Improve
Article Tags :
  • Java
  • Java-I/O
Practice Tags :
  • Java

Similar Reads

  • Java CharArrayWriter Class | Set 2
    In the Java.io.CharArrayWriter class in Java | Set 1, we have already discussed about which CharArrayWriter class and how it works. In this article, we are going to discuss some more methods of the CharArrayWriter class, which give us strong control over handling character data. Java CharArrayWriter
    3 min read
  • CharArrayWriter size() method in Java with examples
    The size() method of the CharArrayWriter class in Java is used to get the current size of the buffer. Syntax: public int size() Parameters: This method does not accept any parameter.Return Value: This method returns an integer value which signifies the current size of the buffer. Below program illus
    2 min read
  • CharArrayWriter append() method in Java with Examples
    The append() method of CharArrayWriter class in Java is of three types: The append(char) method of CharArrayWriter class in Java is used to append the specified character to the writer. This append() method appends one character at a time to the CharArrayWriter and returns this CharArrayWriter.Synta
    3 min read
  • Java.util.ArrayDeque Class in Java | Set 1
    java.util.ArrayDeque class describes an implementation of a resizable array structure which implements the Deque interface. Array deques has not immutable and can grow as necessary. They are not thread-safe and hence concurrent access by multiple threads is not supported unless explicitly synchroniz
    5 min read
  • CharArrayWriter write() method in Java with Examples
    The write() method of CharArrayWriter class in Java is of three types: The write(int) method of CharArrayWriter class in Java is used to write a character to the writer in form of an integer. This write() method writes one character at a time to the CharArrayWriter.Syntax: public void write(int c) O
    3 min read
  • CharArrayWriter writeTo() method in Java with Examples
    The writeTo(Writer) method of CharArrayWriter class in Java is used to write the contents of the CharArrayWriter to another character stream.Syntax: public void writeTo(Writer out) throws IOException Parameters: This method accepts one parameter out that represents the output stream that is the dest
    1 min read
  • Arrays Class in Java
    The Arrays class in java.util package is a part of the Java Collection Framework. This class provides static methods to dynamically create and access Java arrays. It consists of only static methods and the methods of an Object class. The methods of this class can be used by the class name itself. Th
    15 min read
  • CharArrayWriter toString() method in Java with examples
    The toString() method of the CharArrayWriter class in Java converts the given input data to a string. Syntax: public String[] toString() Parameters: This method does not accept any parameter. Return Value: This method returns a copy of the input data. Below program illustrate the above method: Progr
    1 min read
  • Reflection Array Class in Java
    The Array class in java.lang.reflect package is a part of the Java Reflection. This class provides static methods to create and access Java arrays dynamically. It is a final class, which means it can't be instantiated or changed. Only the methods of this class can be used by the class name itself. T
    8 min read
  • Java.util.ArrayDeque Class in Java | Set 2
    Java.util.ArrayDeque Class in Java | Set 1 More Methods of util.ArrayDeque Class : 16. offer(Element e) : java.util.ArrayDeque.offer(Element e) : inserts element at the end of deque. Syntax : public boolean offer(Element e) Parameters : e - element to add Return : true, if element is added else; fal
    5 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences