Java - CharArrayWriter write(char[] c, int off, int len) method



Description

The Java CharArrayWriter write(char[] c, int off, int len) method writes portion of the specified character buffer to the writer.

Declaration

Following is the declaration for java.io.CharArrayWriter.write(char[] c, int off, int len) method −

public void write(char[] c, int off, int len)

Parameters

  • c − The character buffer.

  • off − Offset from which to start reading characters.

  • len − Number of chars to write.

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(char[] c, int off, int len) method.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {      
      char[] ch = {'A','B','C','D','E'};
      CharArrayWriter chw = null;
            
      try {
         // create character array writer
         chw = new CharArrayWriter();
         System.out.println("off = 3; len = 2");
         
         // write character buffer to the writer
         chw.write(ch, 3, 2);
         
         // get buffered content as string
         String str = chw.toString();
         
         // print the string
         System.out.print(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 −

off = 3; len = 2
DE

Example - Writing a Portion of a Character Array

The following example shows the usage of Java CharArrayWriter write(char[] c, int off, int len) 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) {
      // Creating CharArrayWriter instance
      CharArrayWriter writer = new CharArrayWriter();

      // Character array to be written
      char[] data = {'H', 'e', 'l', 'l', 'o', ' ', 'J', 'a', 'v', 'a'};

      // Writing a subset of the array (from index 6, 4 characters: "Java")
      writer.write(data, 6, 4);

      // Converting to string and displaying the result
      System.out.println("Written Output: " + writer.toString());

      // Closing the writer (optional)
      writer.close();
   }
}

Output

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

Written Output: Java

Explanation

  • A CharArrayWriter instance is created.

  • A character array {'H', 'e', 'l', 'l', 'o', ' ', 'J', 'a', 'v', 'a'} is initialized.

  • The write(char[] cbuf, int off, int len) method is used to write a portion of the array starting at index 6 ('J') and writing 4 characters ("Java").

  • The written content is printed as "Java".

Example - Writing Multiple Subsets of a Character Array

The following example shows the usage of Java CharArrayWriter write(char[] c, int off, int len) method. This example demonstrates writing multiple portions of a character array into the CharArrayWriter.

CharArrayWriterDemo.java

package com.tutorialspoint;

import java.io.CharArrayWriter;

public class CharArrayWriterDemo {
   public static void main(String[] args) {

      // Creating CharArrayWriter instance
      CharArrayWriter writer = new CharArrayWriter();

      // Character array to be written
      char[] data = {'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'J', 'a', 'v', 'a'};

      // Writing the first word "Welcome" (0 to 7)
      writer.write(data, 0, 7);

      // Writing " Java" (11 to 4 characters)
      writer.write(data, 11, 4);

      // Converting to string and displaying the result
      System.out.println("Written Output: " + writer.toString());

      // Closing the writer (optional)
      writer.close();
   }
}

Output

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

Written Output: WelcomeJava

Explanation

  • A CharArrayWriter instance is created.

  • A character array {'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 't', 'o', ' ', 'J', 'a', 'v', 'a'} is initialized.

  • The first word "Welcome" (indices 0 to 6, length 7) is written.

  • The word "Java" (starting from index 11, length 4) is appended.

  • The output "Welcome Java" is printed.

java_io_chararraywriter.htm
Advertisements