Try this to read a file:

BufferedReader reader = null;

try {
    File file = new File("sample-file.dat");
    reader = new BufferedReader(new FileReader(file));

    String line;
    while ((line = reader.readLine()) != null) {
        System.out.println(line);
    }

} catch (IOException e) {
    e.printStackTrace();
} finally {
    try {
        reader.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Answer from Matthias Herlitzius on Stack Overflow
🌐
W3Schools
w3schools.com › java › java_bufferedreader.asp
Java BufferedReader
HTML Certificate CSS Certificate JavaScript Certificate Front End Certificate SQL Certificate Python Certificate PHP Certificate jQuery Certificate Java Certificate C++ Certificate C# Certificate XML Certificate ... W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning.
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › BufferedReader.html
BufferedReader (Java Platform SE 8 )
April 21, 2026 - If, however, the buffer is empty, the mark is not valid, and the requested length is at least as large as the buffer, then this method will read characters directly from the underlying stream into the given array. Thus redundant BufferedReaders will not copy data unnecessarily.
Discussions

Java - Reading from a Socket using a BufferedReader object
Hello, I need help understanding ... works in Java. From what I understanding when a BufferedReader object reads from a file using the readline() method it reads the file line by line and when there is nothing left to read the method returns a null (The code labelled Example1 is an illustration ... More on forum.allaboutcircuits.com
🌐 forum.allaboutcircuits.com
4
December 3, 2014
How does BufferedReader work? And lots of other questions about inputs to the system
This construct has been commonly used before the Scanner class became a thing. It is also used to read from files. To answer your questions: in is just a variable name - this is basic Java here. It is a variable declaration. BufferedReader is the data type and in is the variable name. Could be anything. BufferedReader is a class (the capital first letter indicates that as per the Java Code Conventions). InputStreamReader is another class and System.in is the static InputStream that is autocreated in every Java application. It reads from the console (the keyboard in most cases). All these can be found in the Java Documentation and this is the very first place to start searching. A Scanner instance is not always required. There are other ways to read from the keyboard and you just see one in the code you are looking at. A stream in general is a sequence of data. An InputStream is data that is read from somewhere (keyboard, file, network, etc.). The word "Input" only denotes the direction of the stream. Data is read in. The opposite would be an OutputStream. Data is written to somewhere. You're seeing this wrong. The classes can throw an exception that needs to be caught and that is what the try is for. See Exceptions I've been using MOOC to learn Java and I'm 2/3 of the way through and I have never seen any of these things even once. Something is concerning in your learning. Either you are rushing through the course or you are not going through it thoroughly enough. Your first question (#1) indicates that you lack fundamental understanding. #1 is just a simple variable declaration. You should not trip over something like that if you are already 2/3 through. Always consult the Java Documentation first if you encounter new classes. More on reddit.com
🌐 r/learnjava
4
2
February 1, 2018
How do I read a string in BufferedReader for use as a variable? - Processing 2.x and 3.x Forum
I've been trying to understand ... from the BufferedReader references in the online manual but the examples from there don't work in Processing. I'm trying to get a working example from this so I can learn the structure and start utilizing it's functions. Thank you, KevinWorkman, for your response and it's good to know the code can work, but I'm still learning java and don't ... More on forum.processing.org
🌐 forum.processing.org
Using while loop and BufferedReader.
every time readLine is called it waits for you to input a line More on reddit.com
🌐 r/javahelp
6
0
August 20, 2021
🌐
GeeksforGeeks
geeksforgeeks.org › java › java-io-bufferedreader-class-java
Java.io.BufferedReader Class in Java - GeeksforGeeks
BufferedReader br = new BufferedReader(new FileReader("file.txt"), 8192); ... import java.io.*; public class GFG{ public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader( new InputStreamReader(...
Published   November 4, 2025
🌐
Baeldung
baeldung.com › home › java › java io › guide to bufferedreader
Guide to BufferedReader | Baeldung
January 8, 2024 - That’s because markSupported() always returns true for BufferedReader. Of course, we might be able to do the above a bit more elegantly in other ways, and indeed mark and reset aren’t very typical methods. They certainly come in handy, though, when there is a need to look ahead. In this quick tutorial, we’ve learned how to read character input streams on a practical example using BufferedReader.
🌐
Programiz
programiz.com › java-programming › bufferedreader
Java BufferedReader (With Examples)
Once we import the package, here is how we can create the reader. // Creates a FileReader FileReader file = new FileReader(String file); // Creates a BufferedReader BufferedReader buffer = new BufferedReader(file); In the above example, we have created a BufferedReader named buffer with the ...
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › javas-bufferedreader-ready-method-explained-650557c8eb53
Java’s BufferedReader.ready() Method Explained | Medium
January 27, 2025 - The examples below demonstrate cases where ready() is especially helpful. When working with large files, it’s often helpful to process data only when it is available. This helps in situations where the program needs to handle partial data before moving to the next operation. import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileReadExample { public static void main(String[] args) { try (BufferedReader reader = new BufferedReader(new FileReader("large_file.txt"))) { System.out.println("Reading file content:"); while (reader.ready()) { String line = reader.readLine(); System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
🌐
BeginnersBook
beginnersbook.com › 2014 › 01 › how-to-read-file-in-java-using-bufferedreader
How to read file in Java using BufferedReader
September 11, 2022 - Method 1: Using readLine() method of BufferedReader class. ... It reads a line of text. ... It reads a character of text. Since it returns an integer value, it needs to be explicitly cast as char for reading the content of file. ... Here I have two txt files myfile.txt and myfile2.txt.
🌐
All About Circuits
forum.allaboutcircuits.com › home › forums › embedded & programming › programming & languages
Java - Reading from a Socket using a BufferedReader object | All About Circuits
December 3, 2014 - Example 1: ... import java.io.*; class Reader{ public static void main(String[] args){ int lineCount = 0; try { File myFile = new File("Reader.java"); FileReader fileReader = new FileReader(myFile); BufferedReader reader = new BufferedReader(fileReader); String line = null; // *** My question is about the next line **/ while( (line = reader.readLine()) != null ) { System.out.println(++lineCount + " " + line); } reader.close(); } catch (Exception ex) { ex.printStackTrace(); } System.out.println("lineCount = " + lineCount); } }
🌐
Reddit
reddit.com › r/learnjava › how does bufferedreader work? and lots of other questions about inputs to the system
r/learnjava on Reddit: How does BufferedReader work? And lots of other questions about inputs to the system
February 1, 2018 -

I'm looking at this: https://github.com/MathBunny/ccc-solutions/blob/master/Senior%202015/S2/Main.java

And I really can't make sense of the BufferedReader thing. It's the first time I've seen it, I gave it a search but couldn't find anything that explains the fundamentals.

These are the lines that (I think) involve the bufferedreader:


BufferedReader in;

in = new BufferedReader (new InputStreamReader (System.in));

String input = in.readLine();


Not sure if they make sense without context, if not you could look at the link since that's where it's all from.

As you can see there are lots of questions here, I'm not expecting anyone to answer all of them (although I would love you if you did), but even if you can/want to answer just one that'd still be great! All help is appreciated!

-Questions:-

  1. On that first line, what is the "in" in "BufferedReader in;"? I've never seen "in" before and I couldn't find a single thing on it after searching it up.

  2. The "new" makes me think that BufferedReader is some type of object, but what's up with the argument it's creating the object with? It creates the BufferedReader object with another new object called new InputStreamReader (System.in)? I really have no clue what's going on, other than the fact that System.in is just reading input.

  3. Speaking of System.in, I thought reading input required a scanner object? Or is this another way to do it entirely? How does it work? Does it only read ints? Can it be set to a variable?

  4. What in the world is an input stream? Apparently it's an "abstract class is the superclass of all classes representing an input stream of bytes", but fuck if I know what that means. Can someone explain it in English?

  5. I tried putting just these three lines and running them (using Netbeans IDE) and it didn't show any errors, but when I ran the program it said some exception must be caught. So I have to use a try statement? Not that I even know what a try statement is really, but it seems weird that bufferedreader will always give an error no matter what...

Note: I've been using MOOC to learn Java and I'm 2/3 of the way through and I have never seen any of these things even once. In any case, I have a trillion other questions but this is a start. Again, any help whatsoever is greatly appreciated! I really need all the help I can get!

Top answer
1 of 1
5
This construct has been commonly used before the Scanner class became a thing. It is also used to read from files. To answer your questions: in is just a variable name - this is basic Java here. It is a variable declaration. BufferedReader is the data type and in is the variable name. Could be anything. BufferedReader is a class (the capital first letter indicates that as per the Java Code Conventions). InputStreamReader is another class and System.in is the static InputStream that is autocreated in every Java application. It reads from the console (the keyboard in most cases). All these can be found in the Java Documentation and this is the very first place to start searching. A Scanner instance is not always required. There are other ways to read from the keyboard and you just see one in the code you are looking at. A stream in general is a sequence of data. An InputStream is data that is read from somewhere (keyboard, file, network, etc.). The word "Input" only denotes the direction of the stream. Data is read in. The opposite would be an OutputStream. Data is written to somewhere. You're seeing this wrong. The classes can throw an exception that needs to be caught and that is what the try is for. See Exceptions I've been using MOOC to learn Java and I'm 2/3 of the way through and I have never seen any of these things even once. Something is concerning in your learning. Either you are rushing through the course or you are not going through it thoroughly enough. Your first question (#1) indicates that you lack fundamental understanding. #1 is just a simple variable declaration. You should not trip over something like that if you are already 2/3 through. Always consult the Java Documentation first if you encounter new classes.
🌐
Jenkov
jenkov.com › tutorials › java-io › bufferedreader.html
Java BufferedReader
November 7, 2019 - This example tells the Java BufferedReader to skip over the next 24 characters in the BufferedReader. The skip() method returns the actual number of characters skipped.
🌐
Quora
quora.com › What-is-a-BufferedReader-in-Java-and-how-do-we-use-it
What is a BufferedReader in Java, and how do we use it? - Quora
Answer (1 of 5): Java BufferedReader Class The BufferedReader class reads text from a character-input stream, buffering characters so as to provide for the efficient reading of characters, arrays, and lines.Following are the important points about BufferedReader − * The buffer size may be spec...
🌐
University of Texas
cs.utexas.edu › ~mitra › csSpring2004 › cs313 › assgn › BufferedReader.html
Using Buffered Readers
import java.io.*; public class CalcArea { public static void main ( String args[] ) throws IOException { System.out.print ( "Enter the radius: " ); BufferedReader input = new BufferedReader ( new InputStreamReader ( System.in ) ); String inputString = input.readLine(); double radius = Double.parseDouble ( inputString ); double area = 3.14159 * radius * radius; System.out.println ( "Area is: " + area ); } }
🌐
Tutorialspoint
tutorialspoint.com › java › io › java_io_bufferedreader.htm
Java - BufferedReader Class
The first line is read using readLine() method. The mark(100) method marks the current position, allowing up to 100 characters to be read before the mark becomes invalid. After reading another line, the reset() method is called to return the ...
🌐
iO Flood
ioflood.com › blog › bufferedreader-java
BufferedReader Class in Java: Guide to Efficient Text Input
February 20, 2024 - ... Socket socket = new Socket("example.com", 80); BufferedReader reader = new BufferedReader(new InputStreamReader(socket.getInputStream())); String line = reader.readLine(); while (line != null) { System.out.println(line); line = ...
🌐
Processing Forum
forum.processing.org › two › discussion › 25743 › how-do-i-read-a-string-in-bufferedreader-for-use-as-a-variable.html
How do I read a string in BufferedReader for use as a variable? - Processing 2.x and 3.x Forum
The code originally referenced above is apparently the only way to transfer the BufferedReader string to a variable (according to the tutorial), hence the request on how to restructure it in processing. It's the antipathy of the function I'm looking to utilize for my code. I can learn from a functional example.
🌐
Mkyong
mkyong.com › home › java › how to read file in java – bufferedreader
How to read file in Java - BufferedReader - Mkyong.com
April 9, 2019 - package com.mkyong; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class FileExample2 { public static void main(String[] args) { try (FileReader reader = new FileReader("filename.txt"); BufferedReader br = new BufferedReader(reader)) { // read line by line String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { System.err.format("IOException: %s%n", e); } } }
🌐
Medium
medium.com › @AlexanderObregon › javas-bufferedreader-lines-method-explained-1cdc1db4b7b2
Java’s BufferedReader.lines() Method Explained | Medium
December 18, 2024 - The BufferedReader.lines() method works seamlessly with the Java Stream API, making it a convenient option for processing text data in a functional programming style. By converting the file content into a stream of lines, you can chain various stream operations for filtering, mapping, or reducing data without needing intermediate storage or explicit loops. Example — Counting the Number of Lines That Match a Condition
🌐
Edureka
edureka.co › blog › bufferedreader-in-java
BufferedReader In Java | BufferedReader Class Examples | Edureka
June 10, 2021 - Output: enter course: Course is: Java enter course: Course is: stop · This brings us to the end of this article where we have learned how we can read characters from the character-input stream using the BufferedReader class in Java.
🌐
LabEx
labex.io › tutorials › java-efficient-text-processing-with-java-bufferedreader-117473
Java BufferedReader Tutorial | Efficient File Reading | LabEx
In this step, we will read a file with the BufferedReader class by wrapping a FileReader. import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class BufferedReaderDemo { public static void main(String[] args) { String path = "path/to/your/file.txt"; try { FileReader fileReader = new FileReader(path); BufferedReader bufferedReader = new BufferedReader(fileReader); //Creating a BufferedReader by wrapping the FileReader // Add code to read data from the file here bufferedReader.close(); } catch (IOException e) { e.printStackTrace(); } } }