BufferedWriter Class in Java

digitization, transformation, binary

BufferedWriter Class in Java

A BufferedWriter object provides a more efficient way to write just a few characters at a time to a Writer. BufferedWriter objects use a buffer to store output for an associated Writer. In other words, a large number of characters are stored in an internal buffer and only written when the buffer fills up or is explicitly flushed. A BufferedWriter is more efficient than a regular Writer because the data is written to memory, rather than a disk or a network. Minimizing the number of write operations to a disk or the network minimizes the cumulative overhead for these operations.

We should wrap a BufferedWriter around any Writer whose write() operations may be time-consuming or costly, such as a FileWriter or an OutputStreamWriter.

Ads code goes here

This class is very similar to BufferedOutputStream, but it operates on a stream of Java characters instead of a byte stream; this makes it easier to support internationalization.

The structure of the class BufferedWriter is given by

public class java.io.BufferedWriter extends java.io.Writer {
// Constructors
public BufferedWriter(Writer out);
public BufferedWriter(Writer out, int size);
// Instance Methods
public void close();
public void flush();
public void newLine();
public void write(int c);
public void write(char[] cbuf, int off, int len);
public void write(String s, int off, int len);
}

The details of the class structure are given as follows:

public BufferedWriter(Writer out);

public BufferedWriter(Writer out) constructor creates a BufferedWriter that acts on the specified Writer, using a buffer with the default size of 8192 characters.

READ  Class InputStreamReader in Java

Parameter
out – The output stream to buffer.

public BufferedWriter(Writer out, int size);

public BufferedWriter(Writer out, int size) constructor creates a BufferedWriter that acts on the specified Writer, using a buffer that is size bytes long.

Parameter
out – The output stream to buffer.
size – The size of buffer to use.

public void close();

public void close() method closes this BufferedWriter and its underlying Writer.

public void flush();

public void flush() method writes the contents of the buffer to the underlying Writer and calls flush() on the underlying Writer. It is called automatically when the buffer fills up. You can also call it before the buffer is full. This is known as “flushing” the buffer. This method blocks until the underlying write() is complete.

public void newLine();

public void newLine() method method writes the newline character or characters to the stream. It uses System.getProperty(‘line.separator’) to choose the newline appropriate for the run-time system. Calling this method is preferable to explicitly writing a newline character.

public void write(int c);

public void write(int c) method places the low-order 16 bits of the specified value into the buffer. If the buffer is full, it is flushed, and the value c is placed in the newly empty buffer. If the buffer is flushed, this method blocks while the data is written; otherwise, this method returns immediately.

Parameter
c – The value to write.

public void write(char[] cbuf, int off, int len);

public void write(char[] cbuf, int off, int len) method copies len characters from cbuf, starting at off, into the buffer. If there is enough space left in the buffer for the new data, it is copied into the buffer, and the method returns immediately. Otherwise, the buffer is filled and flushed repeatedly until all the new data has been copied into the buffer.

READ  Class InflaterInputStream in Java

Parameter
cbuf – An array of characters to write.
off – An offset into the character array.
len – The number of characters to write.

public void write(String s, int off, int len);

public void write(String s, int off, int len) method copies len characters from s, starting at off, into the buffer. If there is enough space left in the buffer for the new data, it is copied into the buffer and the method returns immediately. Otherwise, the buffer is filled and flushed repeatedly until all the new data has been copied into the buffer.

Parameter
s
The string to be written.
off
An offset into the string.
len
The number of characters to write.
Apart from these BufferedWriter class also has inherited methods from class- Object. They are as follows:

  • clone()
  • finalize()
  • hashCode()
  • notifyAll()
  • wait()
  • wait(long, int)
  • equals(Object)
  • getClass()
  • notify()
  • toString()
  • wait(long)

BufferedWriter class also has inherited methods from Writer. They are as follows:

  • write(char[])
  • write(String)

Share and Enjoy !

Leave a Comment

Your email address will not be published. Required fields are marked *