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



Description

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

If the encoding has an historical name then that name is returned; otherwise the encoding's canonical name is returned.

If this instance was created with the OutputStreamWriter(OutputStream, String) constructor then the returned name, being unique for the encoding, may differ from the name passed to the constructor. This method may return null if the stream has been closed.method flushes the stream.

Declaration

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

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

Parameters

  • cbuf − Buffer 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

IOException − If an I/O error occurs.

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

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      char[] arr = {'H', 'e', 'l', 'l', 'o'};

      try {
         // create a new OutputStreamWriter
         OutputStream os = new FileOutputStream("test.txt");
         OutputStreamWriter writer = new OutputStreamWriter(os);

         // create a new FileInputStream to read what we write
         FileInputStream in = new FileInputStream("test.txt");

         // write something in the file
         writer.write(arr, 0, 3);

         // flush the stream
         writer.flush();

         // read what we write
         for (int i = 0; i < 3; i++) {
            System.out.print("" + (char) in.read());
         }

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

Output

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

Hel

Example - Writing a portion of a character array to a file

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try {
         OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream("char_output1.txt"));

         char[] chars = {'H', 'e', 'l', 'l', 'o', ' ', 'W', 'o', 'r', 'l', 'd'};

         // Write only "World" (from index 6, length 5)
         writer.write(chars, 6, 5);

         writer.close();
         System.out.println("Partial char array written to file.");
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

Partial char array written to file.

Explanation

  • write(char[] cbuf, int off, int len) writes a subset of characters from the array.

  • This example writes the characters from index 6 to 10 - i.e., "World".

  • Useful for selectively writing parts of a char array (e.g., after parsing or formatting).

Example - Writing part of a dynamically created char array

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

OutputStreamWriterDemo.java

package com.tutorialspoint;

import java.io.OutputStreamWriter;
import java.io.IOException;

public class OutputStreamWriterDemo {
   public static void main(String[] args) {
      try (OutputStreamWriter writer = new OutputStreamWriter(System.out)) {
         String message = "Java OutputStreamWriter Example";
         char[] cbuf = message.toCharArray();

         // Write only the word "OutputStreamWriter" (index 5 to 23)
         writer.write(cbuf, 5, 18);
         writer.flush();  // Ensure immediate output to console
      } catch (IOException e) {
         e.printStackTrace();
      }
   }
}

Output

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

OutputStreamWriter

Explanation

  • Converts a String to a char[] using toCharArray().

  • Writes characters starting at index 5 for 18 characters → "OutputStreamWriter".

  • flush() ensures it's printed to the console right away.

java_io_outputstreamwriter.htm
Advertisements