
- 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 - StringWriter write(char[] cbuf,int off,int len) method
Description
The Java StringWriter write(char[] cbuf,int off,int len) method writes a portion of an array of characters.
Declaration
Following is the declaration for java.io.StringWriter.write(char[] cbuf,int off,int len) method.
public String write(char[] cbuf,int off,int len)
Parameters
cbuf − Array 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
NA
Example - Usage of StringWriter write(char[] cbuf,int off,int len) method
The following example shows the usage of StringWriter write(char[] cbuf,int off,int len) method.
StringWriterDemo.java
package com.tutorialspoint; import java.io.StringWriter; public class StringWriterDemo { public static void main(String[] args) { char c = 'b'; // create a new writer StringWriter sw = new StringWriter(); // write chars sw.write('a'); sw.write(c); // print result by converting to string System.out.println("" + sw.toString()); } }
Output
Let us compile and run the above program, this will produce the following result −
ab
Example - Usage of StringWriter write(char[] cbuf,int off,int len) method
The following example shows the usage of StringWriter write(char[] cbuf,int off,int len) method.
StringWriterDemo.java
package com.tutorialspoint; import java.io.StringWriter; public class StringWriterDemo { public static void main(String[] args) { StringWriter sw = new StringWriter(); char[] letters = { 'J', 'a', 'v', 'a', 'W', 'o', 'r', 'l', 'd' }; // Write characters from index 4 to 7: 'W', 'o', 'r', 'l' sw.write(letters, 4, 4); System.out.println("Output: " + sw.toString()); } }
Output
Let us compile and run the above program, this will produce the following result−
Output: Worl
Explanation
The char[] array contains "JavaWorld".
-
The write() method is called with −
off = 4 → starting at 'W'
len = 4 → writing 'W', 'o', 'r', 'l'
Example - Usage of StringWriter write(char[] cbuf,int off,int len) method
The following example shows the usage of StringWriter write(char[] cbuf,int off,int len) method.
StringWriterDemo.java
package com.tutorialspoint; import java.io.StringWriter; public class StringWriterDemo { public static void main(String[] args) { StringWriter sw = new StringWriter(); char[] data = { 'H', 'e', 'l', 'l', 'o', '-', 'J', 'a', 'v', 'a' }; sw.write(data, 6, 4); // writes 'Java' System.out.println("Output: " + sw.toString()); } }
Output
Let us compile and run the above program, this will produce the following result−
Output: Java
Explanation
A portion of the character array is written using write(char[], int, int).
Starts at index 6 and writes 4 characters: 'J', 'a', 'v', 'a'.