
- 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 - FileWriter write(char [] cbuf, int offset, int len) method
Description
The Java FileWriter write(char [] cbuf, int offset, int len) method writes a portion of a character array to a file, starting from a specified offset and writing up to a specified length.
Declaration
Following is the declaration for java.io.FileWriter.write(char [] c, int offset, int len) method −
public void write(char [] c, int offset, 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 any value.
Exception
IndexOutOfBoundsException − If off is negative, or len is negative, or off + len is negative or greater than the length of the given array.
IOException − If an I/O error occurs.
Example - Writing a Subset of a Character Array to a File
The following example shows the usage of Java FileWriter write(char [] c, int offset, int len) method.
FileWriterDemo.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); char[] data = {'H', 'e', 'l', 'l', 'o', ' ', 'J', 'a', 'v', 'a'}; writer.write(data, 0, 5); // Writes "Hello" to the file writer.close(); System.out.println("Data written successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Data written successfully.
Explanation
A FileWriter object is created to write to example.txt.
A character array data is defined.
The write(char[] cbuf, int off, int len) method writes characters from index 0 to 4 ("Hello") into the file.
The file is closed after writing.
Example - Writing from an Offset Position
The following example shows the usage of Java FileWriter write(char [] c, int offset, int len) method.
FileWriterDemo.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); char[] data = {'W', 'e', 'l', 'c', 'o', 'm', 'e', ' ', 'T', 'o', ' ', 'J', 'a', 'v', 'a'}; writer.write(data, 8, 7); // Writes "To Java" to the file writer.close(); System.out.println("Data written successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Data written successfully.
Explanation
The method writes 7 characters starting from index 8, resulting in "To Java" being written to example.txt.
Example - Handling Larger Character Arrays
The following example shows the usage of Java FileWriter write(char [] c, int offset, int len) method.
FileWriterDemo.java
package com.tutorialspoint; import java.io.FileWriter; import java.io.IOException; public class FileWriterDemo { public static void main(String[] args) { try { FileWriter writer = new FileWriter("example.txt"); char[] data = "Learning Java FileWriter!".toCharArray(); writer.write(data, 9, 13); // Writes "Java FileWriter" to the file writer.close(); System.out.println("Data written successfully."); } catch (IOException e) { e.printStackTrace(); } } }
Output
Let us compile and run the above program, this will produce the following result−
Data written successfully.
Explanation
The entire string "Learning Java FileWriter!" is converted into a character array.
The method writes from index 9 ('J') and continues for 13 characters, writing "Java FileWriter" into the file.