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.

java_io_filewriter.htm
Advertisements