Update Java 16:

line =  buffer.lines().toList();

One line of code using Java 8:

line =  buffer.lines().collect(Collectors.joining());
Answer from Russel Yang on Stack Overflow
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › BufferedReader.html
BufferedReader (Java Platform SE 8 )
April 21, 2026 - Reads text from a character-input ... and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any ...
Discussions

java - BufferedReader: read multiple lines into a single string - Stack Overflow
I'm reading numbers from a txt file using BufferedReader for analysis. The way I'm going about this now is- reading a line using .readline, splitting this string into an array of strings using .spl... More on stackoverflow.com
🌐 stackoverflow.com
java - Read all lines with BufferedReader and not empy it - Stack Overflow
BufferedReader object that has data from socket. How to get whole BufferedReader content without erasing it. I need it for tracing purposes. More on stackoverflow.com
🌐 stackoverflow.com
[Java-8] BufferedReader not reading the entire file and not exiting the loop.
You should read your file like this: File file = new File("C:\\Users\\pankaj\\Desktop\\test.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String st; while ((st = br.readLine()) != null) System.out.println(st); } You can also use other ways to read the file. Your loop somehow is missing the readLines and is not considering. Maybe you can change just the last part to read the line always until the readLine is null. change this: if(line.equals("")){ line = br.readLine(); } to simply this: line = br.readLine(); reference: https://www.geeksforgeeks.org/different-ways-reading-text-file-java/ More on reddit.com
🌐 r/learnprogramming
2
1
December 4, 2019
[java] why is bufferedreader missing a line from my txt file?
Line 66 and following: while ((line = inFile.readLine()) != null) { //System.out.println (line); data =inFile.readLine().split("\\s"); You're calling inFile.readLine() twice, so it's eating up 2 lines per iteration. Use the line-variable that you declared in place of the second call. More on reddit.com
🌐 r/learnprogramming
3
1
October 27, 2015
🌐
Medium
medium.com › @AlexanderObregon › javas-bufferedreader-readline-method-explained-66b76877a7e4
Java’s BufferedReader.readLine() Method Explained | Medium
August 30, 2024 - In this example, the readLine() method reads each line from the CSV file, and the line is split into fields using the split() method. This approach allows you to efficiently process large datasets by handling each record individually.
🌐
Baeldung
baeldung.com › home › java › java io › guide to bufferedreader
Guide to BufferedReader | Baeldung
January 8, 2024 - In addition to buffering, BufferedReader also provides some nice helper functions for reading files line-by-line.
🌐
Medium
medium.com › @AlexanderObregon › javas-bufferedreader-lines-method-explained-1cdc1db4b7b2
Java’s BufferedReader.lines() Method Explained | Medium
December 18, 2024 - The lines() method leverages the underlying reader to sequentially read lines of text, converting them into a stream. A stream is a sequence of elements that can be processed using operations like filtering, mapping, or reducing.
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-read-file-line-by-line
Java Read File: Complete Guide with Examples | DigitalOcean
February 20, 2025 - Continue your learning with the BufferedReader API Doc (Java SE 8). You can use the Scanner class to open a file and then read its content line-by-line.
🌐
Tutorialspoint
tutorialspoint.com › java › io › bufferedreader_readline.htm
Java - BufferedReader readLine() method
The BufferedReader is created with a StringReader that contains multiple lines of input. The readLine() method reads one line at a time, stopping at line terminators (\n).
🌐
Cornell Computer Science
cs.cornell.edu › courses › JavaAndDS › files › io3readFile.pdf pdf
Reading a text file
Once a BufferedReader object bf has been created for a file, calling method bf.readLine() reads and returns a line of text. If there are no more lines to read, bf.readLine()returns null. After reading the · file, close the file by calling method bf.close(). We give the pattern that all methods ...
Find elsewhere
🌐
Blogger
javarevisited.blogspot.com › 2012 › 07 › read-file-line-by-line-java-example-scanner.html
How to read file line by line in Java - BufferedReader Scanner Example Tutorial
Output: Reading File line by line using BufferedReader first line in file second line third line fourth line fifth line last line in file · while using FileInputStream or any IO Reader don't forget to close the stream in finally block so that file descriptor associated with this operation get released.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bufferedreader-readline-method-in-java-with-examples
BufferedReader readLine() method in Java with Examples - GeeksforGeeks
May 28, 2020 - The readLine() method of BufferedReader class in Java is used to read one line text at a time. The end of a line is to be understood by '\n' or '\r' or EOF.
🌐
iO Flood
ioflood.com › blog › java-read-line
Java readLine() Method: Proper Usage Guide
February 20, 2024 - We then create a BufferedReader object named ‘reader’ and initialize it with a FileReader object. The FileReader object is used to read the contents of a file (‘file.txt’ in this case). We then read and print each line of the file using a while loop until there are no more lines to read (i.e., readLine() returns null).
🌐
GeeksforGeeks
geeksforgeeks.org › java › bufferedreader-class-lines-method-in-java-with-examples
BufferedReader Class lines() method in Java with Examples - GeeksforGeeks
January 28, 2021 - Example: Find the line containing Hello as a word it will only work in a continuous scanning · The directory contains one txt files named as GFG.txt ... // Java program to demonstrate the continuous scanning // by BufferedReader.lines() method and return the stream // of lines that contains the specific word import java.io.*; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.*; import java.util.Arrays; import java.util.List; import java.util.stream.Stream; class GFG { public static void main(String[] args) { FileReader f= new FileReader("lo
🌐
Reddit
reddit.com › r/learnprogramming › [java-8] bufferedreader not reading the entire file and not exiting the loop.
r/learnprogramming on Reddit: [Java-8] BufferedReader not reading the entire file and not exiting the loop.
December 4, 2019 -

I have ini file that to be read in my application. but the problem is it is not reading the entire file and it stucks in the while loop.

My code:

    FileReader fr = new FileReader(file);
    BufferedReader br = new BufferedReader(fr);

    String line = br.readLine();
    Properties section = null;

    while(line!=null){
         if(line.startsWith("[") && line.endsWith("]")){
             section = new Properties();
             this.config.put(line.substring(1, line.length() - 1), section);
         }else{
             String key = line.split("=")[0];
             String value = line.split("=")[1];
             section.setProperty(key, value);
         }

         line = br.readLine();
         System.out.println(line);

         // To continue reading newline. 
         //if i remove this, it will not continue reading the second header
         if(line.equals("")){ 
             line = br.readLine();
         }  
    }
    
    System.out.println("Done"); // Not printing this.

The text to be read:

    [header]
    key=value

    [header2]
    key1=value1
    key2=value2

    [header3]
    key=value

    // -- stops here
    
    //this newlines are included.


    #Some text
    #Some text

Output:

    [header]
    key=value
    [header2]
    key1=value1
    key2=value2
    [header3]
    key=value
    //whitespace
    //whitespace
🌐
Oracle
docs.oracle.com › en › java › javase › 21 › docs › api › java.base › java › io › BufferedReader.html
BufferedReader (Java SE 21 & JDK 21)
January 20, 2026 - Reads text from a character-input ... and lines. The buffer size may be specified, or the default size may be used. The default is large enough for most purposes. In general, each read request made of a Reader causes a corresponding read request to be made of the underlying character or byte stream. It is therefore advisable to wrap a BufferedReader around any ...
🌐
Oracle
docs.oracle.com › en › java › javase › 11 › docs › api › java.base › java › io › BufferedReader.html
BufferedReader (Java SE 11 & JDK 11 )
January 20, 2026 - After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line. If an IOException is thrown when accessing the underlying BufferedReader, it is wrapped in an UncheckedIOException which will be thrown from the Stream method that caused the read to take place.
🌐
Oracle
docs.oracle.com › javase › 10 › docs › api › java › io › BufferedReader.html
BufferedReader (Java SE 10 & JDK 10 )
After execution of the terminal stream operation there are no guarantees that the reader will be at a specific position from which to read the next character or line. If an IOException is thrown when accessing the underlying BufferedReader, it is wrapped in an UncheckedIOException which will be thrown from the Stream method that caused the read to take place.
🌐
Reddit
reddit.com › r/learnjava › [deleted by user]
[deleted by user] : r/learnjava
February 21, 2023 - In your example, the reader is buffered. So it's not reading character by character. So the line = reader.readLine()reads a line of text (either to the next line-break, or the end of the file, whichever is first), and if no text was available, null is returned.