Signup/Sign In

Java BufferedWriter Class

In this tutorial, we will learn about BufferedWriter class in Java. This class is used to provide buffering for Writer instances. By using this class we can improve the performance. This class maintains an internal buffer of 8192 characters. If we are performing a write operation then it will store data inside the buffer instead of the hard disk and once the buffer gets completely filled it will write the complete buffer to the hard disk.

Syntax

This is the syntax declaration of BufferedWriter class and it is extending Writer class.

public class BufferedWriter extends Writer  

Java BufferedWriter Class Constructors

This class provides two variants of constructors and these are given in the table below.

Constructor Description
BufferedWriter(Writer wrt) It is used to create a buffered character output stream that uses the default size for an output buffer.
BufferedWriter(Writer wrt, int size) It is used to create a buffered character output stream that uses the specified size for an output buffer.

Java BufferedWriter Class Methods

Various methods of the table are given in the table below.

Method Description
void newLine() This method is used to add a new line by writing a line separator.
void write(int c) This method is used to write a single character.
void write(char[] cbuf, int off, int len) This method is used to write a portion of an array of characters.
void write(String s, int off, int len) This method is used to write a portion of a string.
void flush() This method is used to flushes the input stream.
void close() This method is used to closes the input stream.

Example of Java BufferedWriter

In the following example, we are using the write() method from BufferedWriter class in Java. Here we created BufferedWriter with FileWriter, then the default buffer of 8192 characters is created. Here buffer will help to write characters to the files more efficiently.

package studytonight;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			FileWriter writer = new FileWriter("E:\\studytonight\\output.txt");  
			BufferedWriter buffer = new BufferedWriter(writer);  
			buffer.write("Hello Studytonight");  
			buffer.close();  
			System.out.println("Data is written to the file successfully..."); 
		}
		catch(Exception e)
		{
			System.out.print(false);
		}
	}
}


Data is written to the file successfully...

output.txt

Hello Studytonight

Example of Java BufferedWriter

In this example, we are implementing a public void write(String arg, int offset, int length), here the first parameter arg is a String to be written inside the file, offset is an index from where we will start writing to the file, the length is the number of characters of the string to write. and this method doesn't return any value.

package studytonight;
import java.io.BufferedWriter;
import java.io.FileWriter;
public class StudyTonight 
{
	public static void main(String args[])
	{
		try
		{
			FileWriter writer = new FileWriter("E:\\studytonight\\output.txt");  
			BufferedWriter buffer = new BufferedWriter(writer);  
			String str = "Hello Studytonight"; 
            buffer.write(str,5,str.length()-5); 
			buffer.close();  
			System.out.println("Data is written to the file successfully..."); 
		}
		catch(Exception e)
		{
			System.out.print(false);
		}
	}
}


Data is written to the file successfully...

output.txt

 Studytonight

Conclusion

In this tutorial, we learned about BufferedWriter class in Java. The BufferedWriter class of the java.io package can be used with other writers to write data more efficiently.



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.