Java Buffered Writer
BufferWriter Class:
It is used to write the data more efficiently. This class is present in the java.io package, it inherits the data from the Writer class. Writer class is an abstract class.

Writer Class:
It is a class of Java.io package, it represents the set of characters. It is an abstract class.
Writer Subclasses:
- Buffered Writer
- File Writer
- String Writer
- Output Stream Writer
Writer class object creation Syntax:
Writer object1 = new FileWriter();
Note: Here, to create an object we use FileWriter because the Writer class is abstract.
BufferedWriter Class Declaration:
public class BufferedWriter extends Writer
Constructors in BufferedWriter:
- BufferedWriter(Writer w)
- BufferedWriter(Writer w, int s)
BufferedWriter(Writer w):
It generates an output stream without mentioning or considering the size.
Syntax:
BufferedWriter object1 = new BufferedWriter (w (or) file)
BufferedWriter(Writer w, int s): It generates an output stream by mentioning or considering the size.Here, s represents the size of the stream.
Syntax:
BufferedWriter object1 = new BufferedWriter( w (or) file , int s)
BufferedWriter Working Principle: When the write operation operates the characters are instead of writing into disk it writes on the internal Buffer then, a buffer is filled with characters then it writes on the disk
BufferedWriter class Methods:
- newLine ()
- write (int n)
- write (char c[], int start, int length)
- write (String s1, int start, int length)
- flush ()
- close ()
write (int n):
It is used for writing a single character to the internal buffer. The return type is a void, it overrides the write method which is present in the Writer class. It accepts only one parameter i.e, Integer type. It throws IOException which is an Input / Output Exception when an error occurs in Input or Output.
Syntax:
public void write(int n)
Program:
import java.io.*;
public class A
{
public static void main(String[] args)
throws IOException
{
StringWriter object1 = new StringWriter();
BufferedWriter object2 = new BufferedWriter( object1);
object2.write(65);
object2.flush();
System.out.println (object1.getBuffer());
}
}
Output:
A
newLine ():
It includes a new line for the writer class. The return type is void. It does not return any value. It throws IOException i.e, Input / Output Exception when an error occurs in Input or Output.
Syntax:
public void newline()
Program:
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException
{
StringWriter object1 = new StringWriter();
BufferedWriter object2 = new BufferedWriter(object1);
object2.write(65);
object2.newLine();
object2.write(66);
object2.flush();
System.out.println (object1.getBuffer());
}
}
Output:
A
B
write ( char c[], int start, int length):
It writes the characters from a particular array to an internal buffer. It does not return any value When buffer size or length is equal to the size of the array then, it directly uses the main Stream.
It throws Input / Output Exception and Index out of bounds exception. I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than an array.
Syntax:
public int read(char[] c, int start, int length)
- c -It is the Destination buffer
- start – at which index it starts writing characters.
- length -.Several characters
Program:
import java.io.*;
public class Main{
public static void main(String[] args) throws IOException
{
StringWriter object1 = new StringWriter();
BufferedWriter object2 = new BufferedWriter(object1);
char c[] = [‘J’, ’A’, ’V’, ‘A’];
object2 . write(c, 0, 2);
object2 . newLine ();
object2 . write(c,2,2);
object2.flush();
System.out.println (object1.getBuffer());
}
}
OUTPUT:
JA
VA
write (String s1, int start, int length):
It is used to write the part of the string when it is passed through the Writer stream. It throws Input / Output Exception and Index out of bounds exception.
I/O Exception occurs when errors are present in Input or Output, Index Out of bounds exception occurs when the negative length is passed through write method or length greater than string.
Syntax:
public int read(String s1, int start, int length)
s1 -It is the Destination buffer
start – at which index it starts writing characters.
length -.Several characters
Program:
import java.io.*;
class C{
public static void main(String[] args) throws IOException
{
StringWriter object1 = new StringWriter();
BufferedWriter object2 = new BufferedWriter(object1);
object2 . write(“JAVA”, 0, 2);
object2.flush();
System.out.println (object2.getBuffer());
}
}
Output:
JA
flush (): It is used to flush the characters and input stream. It does not return any value.
Syntax:
public void flush()
Program:
import java.io.*;
class C{
public static void main(String[] args) throws IOException
{
StringWriter object1 = new StringWriter();
BufferedWriter object2 = new BufferedWriter(object1);
object2 . write(“JAVA”, 0, 2);
object2.flush();
System.out.println(“Data is flushed”);
System.out.println (object2.getBuffer());
}
}
Output:
Data is flushed.
JA
close (): It uses to close the Buffered Writer class.
Syntax:
public void close()