The write() method in BufferedWriter class in Java is used in three ways:
1. The write(int) method of BufferedWriter class in Java is used to write a single character at a time in the buffer writer stream.
Syntax:
Java
Java
Java
public void write(int ch)
throws IOException
Overrides: This method overrides the write() method of Writer class.
Parameters: This method accept single parameter ch which represents the character that is to be written.
Return value: This method does not return any value.
Exceptions: This method throws IOException if an I/O error occurs.
Below program illustrates write(int) method in BufferedWriter class in IO package:
Program:
// Java program to illustrate
// BufferedWriter write(int) method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create the string Writer
StringWriter stringWriter
= new StringWriter();
// Convert stringWriter to
// bufferedWriter
BufferedWriter buffWriter
= new BufferedWriter(
stringWriter);
// Write "G" to buffer writer
buffWriter.write(71);
// Write "E" to buffer writer
buffWriter.write(69);
// Write "E" to buffer writer
buffWriter.write(69);
// Write "K" to buffer writer
buffWriter.write(75);
// Write "S" to buffer writer
buffWriter.write(83);
buffWriter.flush();
System.out.println(
stringWriter.getBuffer());
}
}
Output:
2. The write(String s, int off, int len) method of BufferedWriter class in Java is used to write a part of a string passed as parameter in the buffer writer stream.
Syntax:
GEEKS
public void write(String s,
int off,
int len)
throws IOException
Overrides: This method overrides the write() method of Writer class.
Parameters: This method accept three parameters:
- s - It represents the string, the part of which is to written.
- off - It represents the index in the string from which writing is started.
- len - It represents the length of portion of string to be written.
- IndexOutOfBoundsException - This method throws IndexOutOfBoundsException if index passed is negative or passed length of portion is greater than the length of the given string.
- IOException - This method throws IOException if IO error occurs.
// Java program to illustrate
// BufferedWriter write(String) method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create the string Writer
StringWriter stringWriter
= new StringWriter();
// Convert stringWriter to
// bufferedWriter
BufferedWriter buffWriter
= new BufferedWriter(
stringWriter);
// Write "GEEKS" to buffer writer
buffWriter.write(
"GEEKSFORGEEKS", 0, 5);
buffWriter.flush();
System.out.println(
stringWriter.getBuffer());
}
}
Output:
3. The write(char[ ] cbuf, int off, int len) method of BufferedWriter class in Java is used to write a part of an array of characters passed as parameter in the buffer writer stream. This method generally stores the characters from the array into the stream and flushes the buffer to the mainstream. It can directly use the mainstream when the length of the buffer is equal to length of the array.
Syntax:
GEEKS
public void write(char[ ] cbuf,
int off,
int len)
throws IOException
Specified By: This method is specified by the write() method of Writer class.
Parameters: This method accept three parameters:
- cbuf - It represents the array of characters, the part of which is to written.
- off - It represents the index of the array from which writing is started.
- len - It represents the length of portion of array to be written.
- IndexOutOfBoundsException - This method throws IndexOutOfBoundsException if index passed is negative or passed length of portion is greater than the length of the given character array.
- IOException - This method throws IOException if IO error occurs.
// Java program to illustrate
// BufferedWriter write(char[ ]) method
import java.io.*;
public class GFG {
public static void main(String[] args)
throws IOException
{
// Create the string Writer
StringWriter stringWriter
= new StringWriter();
// Convert stringWriter to
// bufferedWriter
BufferedWriter buffWriter
= new BufferedWriter(
stringWriter);
// Create character array
char cbuf[] = { 'G', 'E', 'E', 'K',
'S', 'F', 'O', 'R' };
// Write "GEEKS" to buffer writer
buffWriter.write(cbuf, 0, 5);
// Write "FOR" to buffer writer
buffWriter.write(cbuf, 5, 3);
// Again write "GEEKS" to buffer writer
buffWriter.write(cbuf, 0, 5);
buffWriter.flush();
System.out.println(
stringWriter.getBuffer());
}
}
Output:
References:
1. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#write(int)
2. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#write(java.lang.String, int, int)
3. https://docs.oracle.com/javase/10/docs/api/java/io/BufferedWriter.html#write(char%5B%5D, int, int)GEEKSFORGEEKS