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

HQ ¬Ľ Java Tutorial ¬Ľ java.io ¬Ľ BufferedWriter ¬Ľ write(char[] cbuf, int off, int len) method example
Java BufferedWriter write(char[] cbuf, int off, int len) method example 2018-02-16T07:26:46+00:00

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.

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

Contact Info

Singapore

Our Goal

JavaTutorialHQ aims to to be The Ultimate Guide on Java with hundreds of examples from basic to advance Topics.

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close