Java.io.Writer.write() Method

Advertisements


Description

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

Declaration

Following is the declaration for java.io.Writer.write() 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

The following example shows the usage of java.io.Writer.write() method.

package com.tutorialspoint;

import java.io.*;

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();
      }
   }
}

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

helloworld



Advertisements

Advertisements