Java Programming Handbook

    Java BufferedReader and BufferedWriter Classes

    In Java, BufferedReader and BufferedWriter are used for efficient reading and writing of text data. Unlike FileReader or FileWriter, they use an internal buffer to reduce the number of interactions with the disk, which makes them much faster.

    Think of them like this:

    Instead of reading or writing one character at a time (which is slow), they read or write a whole chunk of data at once and keep it in memory. This reduces the time it takes to access the file system again and again.

    They are part of the java.io package and extend the Reader and Writer classes respectively.

    1. Class Hierarchy#

    java.lang.Object ↳ java.io.Reader ↳ java.io.BufferedReader java.lang.Object ↳ java.io.Writer ↳ java.io.BufferedWriter
    • BufferedReader extends Reader
    • BufferedWriter extends Writer

    2. BufferedReader Class#

    ● Constructor#

    BufferedReader(Reader reader) BufferedReader(Reader reader, int bufferSize)

    You usually wrap a BufferedReader around another Reader like FileReader.

    ● Example: Reading line by line using readLine()#

    import java.io.*; public class BufferedReaderReadLineExample { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) { String line; while ((line = reader.readLine()) != null) { System.out.println("Read Line: " + line); } } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Read Line: Hello Read Line: Java Read Line: World

    Explanation:#

    • readLine() reads one line at a time and returns null when end of file is reached.
    • It’s commonly used when working with text data line-by-line.

    int read()#

    Reads a single character.

    import java.io.*; public class BufferedReaderSingleChar { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) { int ch; while ((ch = reader.read()) != -1) { System.out.print((char) ch); } } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Hello Java

    int read(char[] cbuf, int off, int len)#

    Reads characters into a portion of an array.

    import java.io.*; public class BufferedReaderCharArray { public static void main(String[] args) { char[] buffer = new char[50]; try (BufferedReader reader = new BufferedReader(new FileReader("input.txt"))) { int numChars = reader.read(buffer, 0, buffer.length); System.out.println("Characters read: " + new String(buffer, 0, numChars)); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Characters read: Hello Java

    void close()#

    Closes the reader and releases any resources. Automatically handled by try-with-resources.

    import java.io.*; public class BufferedReaderCloseExample { public static void main(String[] args) { BufferedReader reader = null; try { reader = new BufferedReader(new FileReader("input.txt")); System.out.println("First Line: " + reader.readLine()); } catch (IOException e) { e.printStackTrace(); } finally { try { if (reader != null) { reader.close(); System.out.println("Reader closed."); } } catch (IOException e) { e.printStackTrace(); } } } }

    Output:#

    First Line: Hello Reader closed.

    3. BufferedWriter Class#

    ● Constructor#

    BufferedWriter(Writer writer) BufferedWriter(Writer writer, int bufferSize)

    Usually wrapped around a FileWriter.

    void write(String s)#

    Writes a string to the buffer.

    import java.io.*; public class BufferedWriterWriteString { public static void main(String[] args) { try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { writer.write("BufferedWriter writing example"); System.out.println("String written to file."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    String written to file.

    void newLine()#

    Writes a platform-specific newline.

    import java.io.*; public class BufferedWriterNewLine { public static void main(String[] args) { try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { writer.write("Line 1"); writer.newLine(); // Adds a newline writer.write("Line 2"); System.out.println("Lines written with newLine()."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Lines written with newLine().

    File output.txt content:

    Line 1 Line 2

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

    import java.io.*; public class BufferedWriterCharArray { public static void main(String[] args) { char[] chars = "Buffered Writer Test".toCharArray(); try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { writer.write(chars, 0, chars.length); System.out.println("Character array written to file."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Character array written to file.

    void flush()#

    Flushes the buffer, forcing any buffered output to be written.

    import java.io.*; public class BufferedWriterFlushExample { public static void main(String[] args) { try (BufferedWriter writer = new BufferedWriter(new FileWriter("output.txt"))) { writer.write("Flush this content"); writer.flush(); System.out.println("Data flushed manually."); } catch (IOException e) { e.printStackTrace(); } } }

    Output:#

    Data flushed manually.

    void close()#

    Closes the writer. Automatically done in try-with-resources.

    import java.io.*; public class BufferedWriterCloseExample { public static void main(String[] args) { BufferedWriter writer = null; try { writer = new BufferedWriter(new FileWriter("output.txt")); writer.write("This will be saved on close"); } catch (IOException e) { e.printStackTrace(); } finally { try { if (writer != null) { writer.close(); System.out.println("Writer closed."); } } catch (IOException e) { e.printStackTrace(); } } } }

    Output:#

    Writer closed.

    Conclusion#

    In this blog, we explored BufferedReader and BufferedWriter, which improve I/O efficiency using internal buffering. We saw how to read characters and lines, and how to write strings, character arrays, and use new lines and flushing.

    In the next blog, we’ll explore StringReader and StringWriter, which help work with strings as streams internally.

    Last updated on Apr 09, 2025