Java - StringWriter write(char[] cbuf,int off,int len) method



Description

The Java StringWriter write(char[] cbuf,int off,int len) method writes a portion of an array of characters.

Declaration

Following is the declaration for java.io.StringWriter.write(char[] cbuf,int off,int len) method.

public String write(char[] cbuf,int off,int len)

Parameters

  • cbuf − Array of characters.

  • off − Offset from which to start writing characters.

  • len − Number of characters to write.

Return Value

This method does not return a value.

Exception

NA

Example - Usage of StringWriter write(char[] cbuf,int off,int len) method

The following example shows the usage of StringWriter write(char[] cbuf,int off,int len) method.

StringWriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

public class StringWriterDemo {
   public static void main(String[] args) {
      char c = 'b';
      
      // create a new writer
      StringWriter sw = new StringWriter();

      // write chars
      sw.write('a');
      sw.write(c);

      // print result by converting to string
      System.out.println("" + sw.toString());
   }
}

Output

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

ab

Example - Usage of StringWriter write(char[] cbuf,int off,int len) method

The following example shows the usage of StringWriter write(char[] cbuf,int off,int len) method.

StringWriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

public class StringWriterDemo {
   public static void main(String[] args) {
      StringWriter sw = new StringWriter();
      char[] letters = { 'J', 'a', 'v', 'a', 'W', 'o', 'r', 'l', 'd' };

      // Write characters from index 4 to 7: 'W', 'o', 'r', 'l'
      sw.write(letters, 4, 4);

      System.out.println("Output: " + sw.toString());
   }
}

Output

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

Output: Worl

Explanation

  • The char[] array contains "JavaWorld".

  • The write() method is called with −

    • off = 4 → starting at 'W'

    • len = 4 → writing 'W', 'o', 'r', 'l'

Example - Usage of StringWriter write(char[] cbuf,int off,int len) method

The following example shows the usage of StringWriter write(char[] cbuf,int off,int len) method.

StringWriterDemo.java

package com.tutorialspoint;

import java.io.StringWriter;

public class StringWriterDemo {
   public static void main(String[] args) {
      StringWriter sw = new StringWriter();
      char[] data = { 'H', 'e', 'l', 'l', 'o', '-', 'J', 'a', 'v', 'a' };

      sw.write(data, 6, 4);  // writes 'Java'

      System.out.println("Output: " + sw.toString());
   }
}

Output

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

Output: Java

Explanation

  • A portion of the character array is written using write(char[], int, int).

  • Starts at index 6 and writes 4 characters: 'J', 'a', 'v', 'a'.

java_io_stringwriter.htm
Advertisements