Java - BufferedWriter write(char[] cbuf, int off, int len) method



Description

The Java BufferedWriter write(char[] cbuf, int off, int len) method writes a section of character buffer to the writer. Offset off from which to start reading characters and the len is the length of the section from the character buffer. This method writes characters starting from the index off (offset) in the array and writes up to len characters. This method is useful when you want to write a specific subset of characters from a larger character array.

Declaration

Following is the declaration for java.io.BufferedWriter.write(char[] cbuf, int off, int len) method −

public void write(char[] cbuf, int off, int len)

Parameters

  • cbuf − Character array to be written

  • off − Offset off to start reading character buffer

  • len − Number of characters to be written to the stream

Return Value

This method does not return any value.

Exception

IOException − If an I/O error occurs.

Example - Using write(char[] cbuf, int off, int len) method

The following example shows the usage of Java BufferedWriter write(char[] cbuf, int off, int len) method.

BufferedWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.IOException;
import java.io.StringWriter;

public class BufferedWriterDemo {
   public static void main(String[] args) throws IOException {
      StringWriter sw = null;
      BufferedWriter bw = null;
      
      char[] cbuf = "ABCDEFGHIJKLMN".toCharArray();
      
      try {
         // create string writer
         sw = new StringWriter();
         
         //create buffered writer
         bw = new BufferedWriter(sw);
         
         // write from specified character buffer to stream
         bw.write(cbuf, 2, 5);
         
         // forces out the characters to string writer
         bw.flush();
         
         // string buffer is created
         StringBuffer sb = sw.getBuffer();
         
         //prints the string
         System.out.println(sb);
            
      } catch(IOException e) {
         // if I/O error occurs
         e.printStackTrace();
      } finally {
         // releases any system resources associated with the stream
         if(sw!=null)
            sw.close();
         if(bw!=null)
            bw.close();
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

CDEFG

Example - Writing a Portion of a Character Array to a File

The following example shows the usage of Java BufferedWriter write(char[] cbuf, int off, int len) method.

BufferedWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterDemo {
   public static void main(String[] args) {
      String filePath = "example.txt";

      // Create a character array
      char[] buffer = "Hello, BufferedWriter!".toCharArray();

      try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
         // Write a portion of the character array to the file
         writer.write(buffer, 7, 14); // Writes "BufferedWriter" from the array
         System.out.println("Portion of the character array written to the file.");
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }   
}

Output

Let us compile and run the above program, this will produce the following result −

Portion of the character array written to the file.

Output in File (example.txt)

BufferedWriter

Explanation

  • A character array is created from the string "Hello, BufferedWriter!".

  • The write(char[] buf, int off, int len) method writes a portion of the array starting at index 7 and writes 11 characters ("BufferedWriter").

  • The file (example.txt) contains only the substring "BufferedWriter" from the original array.

Example - Writing Multiple Portions of a Character Array

The following example shows the usage of Java BufferedWriter write(char[] cbuf, int off, int len) method.

BufferedWriterDemo.java

package com.tutorialspoint;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;

public class BufferedWriterDemo {
   public static void main(String[] args) {
      String filePath = "example.txt";

      // Create a character array
      char[] buffer = "Java Programming Language".toCharArray();

      try (BufferedWriter writer = new BufferedWriter(new FileWriter(filePath))) {
         // Write the first word
         writer.write(buffer, 0, 4); // Writes "Java"
         writer.newLine(); // Add a line separator

         // Write the second word
         writer.write(buffer, 5, 11); // Writes "Programming"
         writer.newLine(); // Add a line separator

         // Write the third word
         writer.write(buffer, 17, 8); // Writes "Language"
         System.out.println("Multiple portions of the character array written to the file.");
      } catch (IOException e) {
         System.err.println("An error occurred: " + e.getMessage());
      }
   }
}

Output

Let us compile and run the above program, this will produce the following result −

Multiple portions of the character array written to the file.

Output in File (example.txt)

Java
Programming
Language

Explanation

  • A character array is created from the string "Java Programming Language".

  • The write(char[] buf, int off, int len) method is used to write specific portions of the array−

    • "Java": Characters from index 0 to 3 (len = 4).

    • "Programming": Characters from index 5 to 15 (len = 11).

    • "Language": Characters from index 17 to the end of the array (len = 8).

  • The newLine() method is used to separate the words into different lines in the output file.

Key Points About write(char[] buf, int off, int len)

  • Flexible Writing− Allows writing specific portions of a character array, providing fine-grained control over what is written.

  • Offset and Length− The off parameter specifies the starting index, and len determines how many characters to write.

  • Error Handling− Ensure that off and len are within the bounds of the array to avoid IndexOutOfBoundsException.

When to Use This Method

  • When you want to write a subset of characters from a larger character array.

  • In scenarios where data is split into segments, and you need to write only specific parts.

These examples illustrate practical use cases of the write(char[] buf, int off, int len) method, showing how to write portions of character arrays to a file efficiently.

java_io_bufferedwriter.htm
Advertisements