Java - CharArrayWriter write(int c) method



Description

The Java CharArrayWriter write(int c) method writes portion of the specified character buffer to the writer. This method writes a single character to the writer.

Declaration

Following is the declaration for java.io.CharArrayWriter.write(int c) method −

public void write(int c)

Parameters

  • c − int representing a character to be written.

Return Value

The method does not return any value.

Exception

NA

Example - Usage of write() method

The following example shows the usage of Java CharArrayWriter write(int c) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      int i = 0;
      String str = "";
      CharArrayWriter chw = null;
            
      try {
         // create character array writer
         chw = new CharArrayWriter();

         // from integer 65 to 69
         for(i = 65; i < 70; i++) {
         
            // write integer to writer
            chw.write(i);
            
            // get the default character set value
            str = chw.toString();
            
            // print the string
            System.out.println(str);
         }
         
      } 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 −

A
AB
ABC
ABCD
ABCDE

Example - Writing a Single Character Using write(int c) method

The following example shows the usage of Java CharArrayWriter write(int c) method. This example writes a subset of a character array to the CharArrayWriter and prints the result.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();
      writer.write(65); // Writes character 'A' (ASCII value 65)
      writer.write('\n'); // Writes a newline character

      // Convert the internal buffer to a string and print
      System.out.println("Written data: " + writer.toString());
   }
}

Output

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

Written data: A

Explanation

  • CharArrayWriter is instantiated to store character data in memory.

  • write(int c) writes the character with ASCII value 65, which corresponds to 'A'.

  • A newline character ('\n') is also written.

  • The toString() method converts the contents of the internal character buffer into a string and prints it.

Example - Writing Multiple Characters Using write(int c) method

The following example shows the usage of Java CharArrayWriter write(int c) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {
      CharArrayWriter writer = new CharArrayWriter();
      String data = "Hello";

      // Writing each character one by one using write(int c)
      for (int i = 0; i < data.length(); i++) {
         writer.write(data.charAt(i));
      }

      // Convert the internal buffer to a character array
      char[] charArray = writer.toCharArray();

      // Print the character array
      System.out.println("Written data: " + new String(charArray));
   }
}

Output

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

Written data: Hello

Explanation

  • The CharArrayWriter object is used to write characters in memory.

  • The loop iterates over each character in the string "Hello" and writes it using write(int c).

  • The toCharArray() method returns the underlying character array, which is then printed.

java_io_chararraywriter.htm
Advertisements