
- 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 - 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().