0
0
JavaHow-ToBeginner · 3 min read

How to Use BufferedReader in Java: Simple Guide with Examples

Use BufferedReader in Java to read text from an input stream efficiently by wrapping it around a Reader like InputStreamReader. Call methods like readLine() to read text line by line, and always close the reader to free resources.
📐

Syntax

The basic syntax to create a BufferedReader is by wrapping it around a Reader, commonly an InputStreamReader for reading from input streams like the keyboard or files.

  • BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); creates a reader for keyboard input.
  • readLine() reads a line of text.
  • close() frees system resources.
java
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
br.close();
💻

Example

This example reads a line of text from the user and prints it back. It shows how to create a BufferedReader, read input, and close the reader properly.

java
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;

public class BufferedReaderExample {
    public static void main(String[] args) {
        try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
            System.out.print("Enter your name: ");
            String name = br.readLine();
            System.out.println("Hello, " + name + "!");
        } catch (IOException e) {
            System.out.println("Error reading input: " + e.getMessage());
        }
    }
}
Output
Enter your name: Alice Hello, Alice!
⚠️

Common Pitfalls

Common mistakes when using BufferedReader include:

  • Not closing the reader, which can cause resource leaks.
  • Not handling IOException, which is required when reading input.
  • Using read() instead of readLine() when you want to read full lines.

Always use try-with-resources or finally block to close the reader.

java
/* Wrong way: Not closing BufferedReader and ignoring exceptions */
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String input = br.readLine(); // IOException not handled

/* Right way: Using try-with-resources and handling IOException */
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
    String input = br.readLine();
    System.out.println(input);
} catch (IOException e) {
    e.printStackTrace();
}
📊

Quick Reference

MethodDescription
BufferedReader(Reader in)Creates a BufferedReader wrapping another Reader
readLine()Reads a line of text, returns null at end of stream
close()Closes the stream and releases resources
ready()Checks if the stream is ready to be read without blocking

Key Takeaways

Use BufferedReader wrapped around InputStreamReader to read text efficiently.
Always handle IOException when reading input with BufferedReader.
Use readLine() to read full lines of text from input.
Close BufferedReader using try-with-resources to avoid resource leaks.
BufferedReader improves performance by buffering input from the source.