0Pricing
Java Academy · Lesson

BufferedReader Alternative

Faster console reading.

BufferedReader Alternative is a free Java Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Java Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

A Faster Reader

Scanner is convenient but relatively slow for large inputs. For high-volume reading, BufferedReader from java.io is much faster because it reads big chunks at once.

Creating a BufferedReader

Wrap an InputStreamReader around System.in inside a BufferedReader. We use a StringReader here so the examples run without typed input.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader("Hello"));
        System.out.println(reader.readLine());
        reader.close();
    }
}

readLine Returns a String

readLine() reads one whole line as a String, without the line terminator. Unlike Scanner, it always returns text, so you parse numbers yourself.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader("first\nsecond"));
        System.out.println(reader.readLine());
        System.out.println(reader.readLine());
        reader.close();
    }
}

End of Input Is null

When there is no more input, readLine() returns null. This is the signal to stop reading.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader("one line"));
        System.out.println(reader.readLine());
        System.out.println(reader.readLine());
        reader.close();
    }
}

Reading All Lines

Loop while readLine() is not null to process every line of input.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader("a\nb\nc"));
        String line;
        while ((line = reader.readLine()) != null) {
            System.out.println("> " + line);
        }
        reader.close();
    }
}

Parsing Numbers Yourself

Since readLine gives text, convert it with Integer.parseInt when you need a number.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader("42"));
        int n = Integer.parseInt(reader.readLine().trim());
        System.out.println(n * 2);
        reader.close();
    }
}

Splitting a Line Into Tokens

To read several numbers from one line, split the line with split and parse each piece.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new StringReader("10 20 30"));
        String[] parts = reader.readLine().split(" ");
        int sum = 0;
        for (String p : parts) {
            sum += Integer.parseInt(p);
        }
        System.out.println(sum);
        reader.close();
    }
}

Checked Exceptions

readLine declares a checked IOException, so you must either declare throws IOException or wrap the call in a try-catch. Scanner does not require this.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        try {
            BufferedReader reader = new BufferedReader(new StringReader("data"));
            System.out.println(reader.readLine());
            reader.close();
        } catch (IOException e) {
            System.out.println("Read error");
        }
    }
}

Try-With-Resources

Use try-with-resources so the reader closes automatically even if an exception occurs.

import java.io.BufferedReader;
import java.io.StringReader;
import java.io.IOException;

public class Main {
    public static void main(String[] args) throws IOException {
        try (BufferedReader reader = new BufferedReader(new StringReader("auto-closed"))) {
            System.out.println(reader.readLine());
        }
    }
}

A Real System.in Example

For real keyboard input you wrap System.in. Run this locally and type a line then press Enter.

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter text: ");
        String input = reader.readLine();
        System.out.println("You typed: " + input);
    }
}

Scanner Versus BufferedReader

Choose Scanner for convenience and easy number parsing. Choose BufferedReader for speed with large inputs, accepting that you parse and handle IOException yourself.

Quick Check

Test your BufferedReader knowledge.

Recap

You learned the BufferedReader alternative:

  • Wrap System.in in InputStreamReader then BufferedReader
  • readLine() returns a whole line, or null at end of input
  • Parse numbers yourself with Integer.parseInt and split
  • It throws checked IOException, so handle or declare it
  • Prefer it over Scanner for large, performance-critical input

Frequently asked questions

Is the “BufferedReader Alternative” lesson free?

Yes — the full text of “BufferedReader Alternative” is free to read here on the web, and the Java Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Java Academy course, upgrade to CoddyKit PRO.

What will I learn in “BufferedReader Alternative”?

Faster console reading. You practise Java Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Java Academy?

No prior experience is required. Java Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “BufferedReader Alternative” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Java Academy lesson?

Yes. Every Java Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Reading Input with Scanner
  2. Reading Numbers and Lines
  3. Validating User Input
  4. BufferedReader Alternative
← Back to Java Academy