Java.io.Writer.write() Method

Advertisements


Description

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

Declaration

Following is the declaration for java.io.Writer.write() method

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

Parameters

  • cbuf -- Array of characters to be written

  • off -- Offset from which to start writing characters

  • len -- Number of characters to write

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[] c = {'h', 'e', 'l', 'l', 'o', 'w', 'o', 'r', 'l', 'd'};

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

      try {
         // write a portion of a char array
         writer.write(c, 0, 5);

         // flush the writer
         writer.flush();

         // write another portion of a char array
         writer.write(c, 5, 5);

         // 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