BufferedWriter class is applied when there is a need buffer for Writer instance. BufferedWriter's major goal is to make programs more efficient while utilizing single characters, arrays, and strings. It inherits the Writer class. Like the buffered reader, BufferedWriter also has a buffer size that the user specifies, or the default size may be used. And the default size is large enough to fulfill most of the applications. Let’s understand buffer in brief.
What is Buffer?
A buffer is a section of memory storage that temporarily holds data and so makes use of RAM (random access memory). Because it stores temporary data, retrieving the stored data is similarly quick. Following is the example of Class Declaration for BufferedWriter
public class BufferedWriter extends Writer
How to Create a BufferedWriter?
1. First import the following package:
2. After the package is imported, we have to write the following code:
ileWriter a = new FileWriter(String name); BufferedWriter b = new BufferedWriter(a);
The first line creates FileWriter with name a, and the second line of code creates BufferedWriter name b.
3. In the above code, we have created BufferedWriter, which has a default size(8192 characters)
We can also define the size of the buffer by adding size (which is a positive integer) beside the file name.
Constructors of BufferedWriter Class
1. BufferedWriter(Writer Out)
This constructor creates a buffered class that has default size.
2. BufferedWriter( Writer out, int n)
This constructor creates a new buffered class with a size defined by the user. The above keyword "n" indicates the output buffer size, and it should be a positive integer.
Methods of BufferedWriter Class
It gives the following methods, such as:
1. Write: It is divided into the following sections, such as:
- Public void write (int a) throws IOException
When only a single character needs to be written, this is the method to employ. Int a specifies a character to be written.
- Public void writes (char() arr, int o, int l) throws IOException.
This function generally inserts characters from the supplied array into this stream's buffer, flushing the buffer as needed to the underlying stream. If the desired length is at least as long as the buffer, this method will flush the buffer and write the characters directly to the underlying stream. It also does not return any value; instead, it flushes the characters and then closes the loop.
arr: character array.
Int o: offset from where to initiate reading.
Int l: indicates the number of characters to be typed
- Public void write(String a, int o, int l)
No characters are printed if the length is negative. Int o from where to initiate reading. The number of characters is denoted by the letter l.
2. newLine: It doesn't return any value. It is used to separate a line.
public void newLine() throws IOexception
3. Flush: It flushes the buffer's character and doesn't return any value.
Public void flush() throws IOException
4. Close: It also doesn't return any value; instead, it flushes the characters before closing the window. So basically first flush() method is used, and then the close() method is implemented.
Public void close()
5. Append: It is used to insert a specified character.
Public void append()
The following code is an example of BufferedWriter.
We don't see anything in the output when we run the above code (except the line we have printed)
However, we can see the result when we go back to our text file named apple.
Difference between BufferedWriter and FileWriter
Let's look at the differences between BufferedWriter and FileWriter.
|It is simple of all the options to write a file.||Similar to FileWriter but uses buffer concept.|
|Comparatively slower.||Comparatively faster.|
|It is used when the number of write operations is less.||It should be used when the number of write operations is more.|
Apart from BufferedWriter and FileWriter, java uses two more ways to write a file operation.
- FileOutputStream: The above two options are designed to write text to files, but when we need raw stream data to be written into a file, we use FileOutputStream.
- Files: The Files utility class was added in Java 7, and we may use its write method to create a file. Internally, it writes a byte array to a file using OutputStream.
If we need multiple writes between flush/close and the writes are small compared to the buffer size, BufferedWriter is what we need to make our code efficient.