There is also Scanner. You can use it just like the BufferedReader:

Scanner scanner = new Scanner(myString);
while (scanner.hasNextLine()) {
  String line = scanner.nextLine();
  // process the line
}
scanner.close();

I think that this is a bit cleaner approach that both of the suggested ones.

Answer from notnoop on Stack Overflow
Discussions

In Java, how do you make the Scanner read an entire line from the user?
Can you post your code please? More on reddit.com
๐ŸŒ r/learnprogramming
4
2
December 4, 2020
string - How to read a file line by line with Java Stream - Stack Overflow
@zakzak That depends on how your ... String. 2018-11-19T12:25:17.757Z+00:00 ... Save this answer. ... Show activity on this post. The method lines() you are using already does what you expect it to do. Java 8 has added a new method called lines() in Files class which can be used to read a file line by line in ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
What is the best way to iterate over the lines of a Java String? - Stack Overflow
Before Java 11, BufferedReader+StringReader the fastest method to read a String line by line. But with Java 11+, it's faster to use String.lines(). More on stackoverflow.com
๐ŸŒ stackoverflow.com
August 16, 2015
Need to read integer and String from a file in java
I think the problem is that the last call to reader.next() that is reading the year is not advancing the reader to the next line. So in the next iteration of the loop the first call to reader.nextLine() for the name is getting the rest of the current line, which is an empty string. Then the nextLine that the reader gets is "MockingJay" which is getting passed to Integer.parseInt() and throwing this exception. Check the JavaDoc for how Scanner.next() and nextLine() differ. More on reddit.com
๐ŸŒ r/learnprogramming
6
2
December 3, 2021
๐ŸŒ
DigitalOcean
digitalocean.com โ€บ community โ€บ tutorials โ€บ java-read-file-line-by-line
Java Read File: Complete Guide with Examples | DigitalOcean
February 20, 2025 - Here is an example program to read a file line-by-line with BufferedReader: ... package com.journaldev.readfileslinebyline; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileLineByLineUsingBufferedReader { public static void main(String[] args) { BufferedReader reader; try { reader = new BufferedReader(new FileReader("sample.txt")); String line = reader.readLine(); while (line != null) { System.out.println(line); // read next line line = reader.readLine(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } }
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ reading-a-file-line-by-line-in-java
Reading a File Line by Line in Java
July 24, 2023 - List<String> lines = FileUtils.readLines(file, "UTF-8"); for (String line: lines) { // process the line } Remember, that this approach reads all lines from the file into the lines list and only then the execution of the for loop starts. It might take a significant amount of time, and you should think twice before using it on large text files. There are multiple ways of reading a file line by line in Java, and the selection of the appropriate approach is entirely a programmer's decision.
๐ŸŒ
Java-tips
java-tips.org โ€บ home โ€บ java se tips โ€บ java.io โ€บ how to read a string line by line
How to read a String line by line - Java Tips
Assume that you have a text area and you want to process its content line by line. In such a case you can easily use StringReader class.
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java 8 โ€บ java read a file line by line
Java Read a File Line by Line
August 22, 2023 - By default, bytes from the file are decoded into characters using the UTF-8 charset. Note that Files.readAllLines does not require explicit resource closing after we have read the file. Path filePath = Paths.get("c:/temp", "data.txt"); List<String> lines = Files.readAllLines(filePath); for (String line : lines) { System.out.println(line); } Till Java 7, we could read a file using FileReader in various ways.
๐ŸŒ
CodeJava
codejava.net โ€บ java-se โ€บ file-io โ€บ how-to-read-text-file-line-by-line-in-java
How to read text file line by line in Java
July 28, 2019 - [Master REST API Development and Java and Spring Boot] In Java File I/O programming, the classes BufferedReader and LineNumberReader allows reading a sequence of lines from a text file, in a line-by-line fashion, by using their readLine() method. The LineNumberReader is a subclass of the ...
Find elsewhere
๐ŸŒ
Javatpoint
javatpoint.com โ€บ how-to-read-file-line-by-line-in-java
How to read file line by line in Java - Javatpoint
How to read file line by line in Java with oops, string, exceptions, multithreading, collections, jdbc, rmi, fundamentals, programs, swing, javafx, io streams, networking, sockets, classes, objects etc,
๐ŸŒ
Attacomsian
attacomsian.com โ€บ blog โ€บ how-to-read-a-file-line-by-line-in-java
How to read a file line by line in Java
February 17, 2020 - The readUtf8Line() method reads the data until the next line delimiter โ€“ either \n, \r\n, or the end of the file. It returns that data as a string, omitting the delimiter at the end.
๐ŸŒ
CodeSignal
codesignal.com โ€บ learn โ€บ courses โ€บ fundamentals-of-text-data-manipulation-in-java-1 โ€บ lessons โ€บ reading-text-files-line-by-line-in-java
Reading Text Files Line-by-Line in Java
To read a file line-by-line in Java, you can use the Files.readAllLines method to read the lines into a List<String>, and then iterate over the list using a for-each loop.
๐ŸŒ
Vultr Docs
docs.vultr.com โ€บ java โ€บ examples โ€บ read-the-content-of-a-file-line-by-line
Java Program to Read the Content of a File Line by Line | Vultr Docs
December 17, 2024 - This approach returns a Stream<String> making it possible to use the powerful stream operations for processing the file content. Itโ€™s especially useful for large files or when performing complex filtering and manipulation of file data. Deploy Scanner for its flexibility with delimiters and its ability to parse primitives directly from text input. Configure Scanner to use a newline delimiter for reading the file line by line. ... import java.io.File; import java.io.FileNotFoundException; import java.util.Scanner; public class ScannerExample { public static void main(String[] args) { File file = new File("example.txt"); try (Scanner scanner = new Scanner(file)) { scanner.useDelimiter(System.lineSeparator()); while (scanner.hasNext()) { System.out.println(scanner.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } } } Explain Code
๐ŸŒ
Reddit
reddit.com โ€บ r/learnprogramming โ€บ in java, how do you make the scanner read an entire line from the user?
r/learnprogramming on Reddit: In Java, how do you make the Scanner read an entire line from the user?
December 4, 2020 -

Trying to make a program which uses the scanner to read an entire series of characters entered by the user before they press enter.

So my code looks something like this: String input = sc.next();

Which prompts the user to enter something, and that gets stored into the string input.

But if the user enters something like - A B C D. In one line. The scanner only stores the A into the string.

How do I make it store the entire line?

When I try using sc.nextLine(), the program simply skips over the prompt. It doesn't prompt the user, it just skips that line and the string comes out empty.

๐ŸŒ
CodingTechRoom
codingtechroom.com โ€บ question โ€บ read-string-line-by-line-java
How to Read a String Line by Line in Java? - CodingTechRoom
Using the `BufferedReader` with `StringReader` for efficient reading of lines in a string. Utilizing Java Stream API for concise and functional-style processing of lines. Mistake: Not handling exceptions thrown by BufferedReader methods.
๐ŸŒ
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
* This Java program demonstrate line by line reading using BufferedReader in Java ... * @author Javin Paul */ public class BufferedReaderExample { public static void main(String args[]) { //reading file line by line in Java using BufferedReader FileInputStream fis = null; BufferedReader reader = null; try { fis = new FileInputStream("C:/sample.txt"); reader = new BufferedReader(new InputStreamReader(fis)); System.out.println("Reading File line by line using BufferedReader"); String line = reader.readLine(); while(line != null){ System.out.println(line); line = reader.readLine(); } } catch (Fil
๐ŸŒ
Baeldung
baeldung.com โ€บ home โ€บ java โ€บ java string โ€บ read stringbuilder line by line in java
Read StringBuilder Line by Line in Java | Baeldung
November 6, 2025 - Another practical way to handle a StringBuilder per line is to wrap its content in a StringReader and use a Scanner to read it line by line.
๐ŸŒ
Java String
javastring.net โ€บ home โ€บ java string lines() method to get the stream of lines
Java String lines() Method to Get the Stream of Lines
July 24, 2019 - package net.javastring.strings; ...ln(lines); } } ... We can count the number of lines in a string using the lines() method followed by the Stream count() method....
๐ŸŒ
HowToDoInJava
howtodoinjava.com โ€บ home โ€บ java 11 โ€บ string lines() โ€“ get stream of lines โ€“ java 11
Java 11 String.lines() - Stream of lines - HowToDoInJava
January 25, 2022 - import java.io.IOException; import java.util.stream.Stream; public class Main { public static void main(String[] args) { try { String str = "A \n B \n C \n D"; Stream<String> lines = str.lines(); lines.forEach(System.out::println); } catch (IOException e) { e.printStackTrace(); } } } Program output. ... Drop me your questions related to reading a string into lines of stream.