
- Java.io - Home
- Java.io - BufferedInputStream
- Java.io - BufferedOutputStream
- Java.io - BufferedReader
- Java.io - BufferedWriter
- Java.io - ByteArrayInputStream
- Java.io - ByteArrayOutputStream
- Java.io - CharArrayReader
- Java.io - CharArrayWriter
- Java.io - Console
- Java.io - DataInputStream
- Java.io - DataOutputStream
- Java.io - File
- Java.io - FileDescriptor
- Java.io - FileInputStream
- Java.io - FileOutputStream
- Java.io - FilePermission
- Java.io - FileReader
- Java.io - FileWriter
- Java.io - FilterInputStream
- Java.io - FilterOutputStream
- Java.io - FilterReader
- Java.io - FilterWriter
- Java.io - InputStream
- Java.io - InputStreamReader
- Java.io - LineNumberInputStream
- Java.io - LineNumberReader
- Java.io - ObjectInputStream
- Java.io - ObjectInputStream.GetField
- Java.io - ObjectOutputStream
- io - ObjectOutputStream.PutField
- Java.io - ObjectStreamClass
- Java.io - ObjectStreamField
- Java.io - OutputStream
- Java.io - OutputStreamWriter
- Java.io - PipedInputStream
- Java.io - PipedOutputStream
- Java.io - PipedReader
- Java.io - PipedWriter
- Java.io - PrintStream
- Java.io - PrintWriter
- Java.io - PushbackInputStream
- Java.io - PushbackReader
- Java.io - RandomAccessFile
- Java.io - Reader
- Java.io - SequenceInputStream
- Java.io - SerializablePermission
- Java.io - StreamTokenizer
- Java.io - StringBufferInputStream
- Java.io - StringReader
- Java.io - StringWriter
- Java.io - Writer
- Java.io package Useful Resources
- Java.io - Discussion
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.