java.io.BufferedWriterwrite(char[] cbuf, int off, int len)

Description

On this document we will be showing a java example on how to use the write(char[] cbuf, int off, int len) method of BufferedWriter Class. The write() method is overloaded. It accepts different method parameters. Below are the overloaded write() methods:

  • write(int c)
  • write(char[] cbuf, int off, int len)
  • write(String s, int off, int len)

On this java tutorial we will be discussing the method write(char[] cbuf, int off, int len). This method Writes a portion of an array of characters.

Ordinarily this method stores characters from the given array into this stream’s buffer, flushing the buffer to the underlying stream as needed. If the requested length is at least as large as the buffer, however, then this method will flush the buffer and write the characters directly to the underlying stream. Thus redundant BufferedWriters will not copy data unnecessarily.

A quick background on Method Overloading

Method overloading is one of the feature of java where we can use the same method name but with different method signature.

Method Syntax

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

Method Argument

Data Type Parameter Description
char[] cbuf character array
int off Offset from which to start reading characters
int len Number of characters to write

Method Returns

This method returns void.

Compatibility

Requires Java 1.1 and up

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

Below is a java code demonstrates the use of write(char[] cbuf, int off, int len) method of BufferedWriter class. The example presented might be simple however it shows the behaviour of the write(char[] cbuf, int off, int len) method. So basically this method expects a character array as the first input. The character array are the set of characters to be written. The second method argument off is basically just the offset. Finally, the third argument is basically the length or count of characters from the cbuff character array to write on a file. To write all the characters of cbuff character array, the len value should be cbuf.length.

package com.javatutorialhq.java.examples;


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

/*
 * A java example source code to demonstrate
 * the use of write((char[] cbuf, int off, int len) method of BufferedWriter class
 */

public class BufferedWriterWriteExample {

	public static void main(String[] args) {	
		
		//initialize a FileWriter
		FileWriter fw;
		try {
			fw = new FileWriter("C:/javatutorialhq/test.txt");
			// initialize our BufferedWriter
			BufferedWriter bw = new BufferedWriter(fw);
			System.out.println("Starting the write operation");
			// write values
			
			
			String s = "test string";
			char[] cbuf = s.toCharArray();
			bw.write(cbuf,0,4);			
			
			// close the BufferedWriter object to finish operation
			bw.close();
			System.out.println("Finished");
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
	}
}

Sample Output

Below is the sample output when you run the above example.

java lang BufferedWriter write((char[] cbuf, int off, int len) example output