Java - Writer write(char[] cbuf) method



Description

The Java Writer write(char[] cbuf) method writes an array of characters.

Declaration

Following is the declaration for java.io.Writer.write(char[] cbuf) method.

public void write(char[] cbuf)

Parameters

cbuf − Array of characters to be written.

Return Value

This method does not return a value.

Exception

  • IOException − If an I/O error occurs.

Example - Usage of Writer write(char[] cbuf) method

The following example shows the usage of Writer write(char[] cbuf) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      char[] c1 = {'h', 'e', 'l', 'l', 'o'};
      char[] c2 = {'w', 'o', 'r', 'l', 'd'};

      // create a new writer
      Writer writer = new PrintWriter(System.out);

      try {
         // write a char array
         writer.write(c1);

         // flush the writer
         writer.flush();

         // write a new char array
         writer.write(c2);

         // flush the stream again
         writer.close();

      } catch (IOException ex) {
         ex.printStackTrace();
      }
   }
}

Output

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

helloworld

Example - Write a Substring from a String

The following example shows the usage of Writer write(char[] cbuf) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) throws IOException {
      Writer writer = new StringWriter();

      String str = "WelcomeToJava";
      writer.write(str, 7, 4);  // Writes "ToJa"

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

Output

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

Output: ToJa

Explanation

  • The string "WelcomeToJava" has characters at index 7 to 10: 'T', 'o', 'J', 'a'.

  • write(str, 7, 4) writes "ToJa" to the Writer.

Example - Extract and Write a Word

The following example shows the usage of Writer write(char[] cbuf) method.

WriterDemo.java

package com.tutorialspoint;

import java.io.IOException;
import java.io.StringWriter;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) throws IOException {
      Writer writer = new StringWriter();

      String sentence = "Java programming is powerful";
      writer.write(sentence, 5, 11);  // Writes "programming"

      System.out.println("Result: " + writer.toString());
   }
}

Output

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

Result: programming

Explanation

  • The string "Java programming is powerful" starts the word "programming" at index 5.

  • Length = 11 → captures "programming".

  • write(sentence, 5, 11) writes "programming" to the buffer.

java_io_writer.htm