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



Description

The Java 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(char[] cbuf,int off,int len) 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 - Usage of Writer write(char[] cbuf,int off,int len) method

The following example shows the usage of Writer write(char[] cbuf,int off,int len) 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[] 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();
      }
   }
}

Output

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

helloworld

Example - Using FileWriter with write(char[], int, int)

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

WriterDemo.java

package com.tutorialspoint;

import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

public class WriterDemo {
   public static void main(String[] args) {
      try {
         Writer writer = new FileWriter("partial1.txt");

         char[] message = "Hello, World!".toCharArray();
         writer.write(message, 7, 5);  // Writes "World"

         writer.close();
         System.out.println("Selected characters written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Selected characters written to file.

Explanation

  • "Hello, World!" is converted to a char[].

  • writer.write(message, 7, 5) writes 5 characters starting from index 7 ('W') − outputs "World".

  • Only a portion of the array is written.

Example - Using StringWriter with write(char[], int, int)

The following example shows the usage of Writer write(char[] cbuf,int off,int len) 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) {
      try {
         Writer writer = new StringWriter();

         char[] data = "Java Programming".toCharArray();
         writer.write(data, 5, 11);  // Writes "Programming"

         System.out.println("Output: " + writer.toString());
         writer.close();
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Output: Programming

Explanation

  • A char[] representing "Java Programming" is used.

  • writer.write(data, 5, 11) starts at index 5 ('P') and writes 11 characters − outputs "Programming".

  • StringWriter stores output in memory, accessible via toString().

java_io_writer.htm
Advertisements