![]() |
VOOZH | about |
BufferedWriter class in Java is used to write text efficiently to character-based output streams. It stores characters in a buffer before writing them to the destination, thereby reducing the number of I/O operations and improving performance.
public class BufferedWriter extends Writer
BufferedWriter class extends the Writer class, meaning it is a subclass specialized for buffered character output operations.
Example: Writing Text to a File
File written successfully.
Explanation: This program creates a BufferedWriter that wraps a FileWriter to write text efficiently to output.txt. The write() method writes text, and newLine() inserts a line break.
1. BufferedWriter(Writer out): Creates a buffered character output stream using the specified writer.
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"));
2. BufferedWriter(Writer out, int size): Creates a buffered character output stream with a custom buffer size.
BufferedWriter bw = new BufferedWriter(new FileWriter("file.txt"), 8192);
Example: Writing User Input to a File
Output:
output.txt:
| Method | Action |
|---|---|
| close() | Closes the stream and releases system resources. Flushes the buffer before closing. |
| flush() | Forces any buffered characters to be written to the output stream. |
| newLine() | Writes a platform-dependent line separator. |
| write(int c) | Writes a single character. |
| write(char[] cbuf, int off, int len) | Writes a portion of a character array. |
| write(String s, int off, int len) | Writes a portion of a string. |