I advise you to go with Scanner instead of DataInputStream. Scanner is specifically designed for this purpose and introduced in Java 5. See the following links to know how to use Scanner.

  • Java Documentation
  • Java Tutorial

Example

Scanner s = new Scanner(System.in);
System.out.println(s.nextInt());
System.out.println(s.nextInt());
System.out.println(s.next());
System.out.println(s.next());
Answer from Jomoos on Stack Overflow
🌐
DigitalOcean
digitalocean.com › community › tutorials › java-read-file-line-by-line
Java Read File: Complete Guide with Examples | DigitalOcean
February 20, 2025 - 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(); } } }
🌐
W3Schools
w3schools.com › java › java_files_read.asp
Java Read Files
Variables Print Variables Multiple Variables Identifiers Constants (Final) Real-Life Examples Code Challenge Java Data Types
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › io › BufferedReader.html
BufferedReader (Java Platform SE 8 )
April 21, 2026 - public String readLine() throws IOException · Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed. Returns: A String containing the contents of the line, not including ...
🌐
GeeksforGeeks
geeksforgeeks.org › java › console-readline-method-in-java-with-examples
Console readLine() method in Java with Examples - GeeksforGeeks
June 12, 2020 - Below programs illustrate readLine(String, Object) method in Console class in IO package: Program 1: ... // Java program to illustrate // Console readLine(String, Object) method import java.io.*; public class GFG { public static void main(String[] args) { // Create the console object Console cnsl = System.console(); if (cnsl == null) { System.out.println( "No console available"); return; } String fmt = "%1$4s %2$10s %3$10s%n"; // Read line String str = cnsl.readLine( fmt, "Enter", "string : "); // Print line System.out.println( "You entered : " + str); } }
🌐
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
Imagine needing to process a set of numbers stored in a file — reading each integer and calculating their sum. This is a practical task you can easily accomplish using Files.readAllLines and basic string manipulation in Java.
Find elsewhere
🌐
Medium
medium.com › @AlexanderObregon › javas-files-readalllines-method-explained-14312314c1c4
Medium
July 27, 2024 - import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class TraditionalFileReader { public static void main(String[] args) { List<String> lines = new ArrayList<>(); try (BufferedReader br = new BufferedReader(new FileReader("example.txt"))) { String line; while ((line = br.readLine()) != null) { lines.add(line); } } catch (IOException e) { e.printStackTrace(); } lines.forEach(System.out::println); } } In this example, reading a file involves creating a BufferedReader, reading lines in a loop, and handling exceptions.
🌐
Baeldung
baeldung.com › home › java › java io › how to read a file in java
How to Read a File in Java | Baeldung
January 8, 2024 - Note that readLine() will return null when the end of the file is reached. In JDK7, the NIO package was significantly updated. Let’s look at an example using the Files class and the readAllLines method. The readAllLines method accepts a Path. Path class can be considered an upgrade of the java.io.File with some additional operations in place.
🌐
Scaler
scaler.com › home › topics › readline() in java
readLine() in Java - Scaler Topics
August 5, 2022 - The readLine() method of BufferedReader class is used to read a one line text from a given input source for example, a file.
🌐
Coderanch
coderanch.com › t › 276242 › java › read-lines-file
how to read lines from a file (I/O and Streams forum at Coderanch)
Please let me know what modifications ... br_reader.readLine(); System.out.println("Line ::"+strin); }catch(IOException objIOException) {} } } Thanks Sravam [ August 07, 2003: Message edited by: sravan reddy ] ... As is, it should basically work and display the first line of the file (you'd want to add "import java.io.*;" at ...
🌐
Medium
medium.com › @AlexanderObregon › javas-bufferedreader-readline-method-explained-66b76877a7e4
Java’s BufferedReader.readLine() Method Explained | Medium
August 30, 2024 - Here’s a basic example of how to use BufferedReader.readLine(): import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; public class ReadFileExample { public static void main(String[] args) { try (BufferedReader br = new BufferedReader(new FileReader("largefile.txt"))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } }
🌐
Educative
educative.io › answers › reading-the-nth-line-from-a-file-in-java
Reading the Nth line from a file in Java
import java.io.BufferedReader; class FileRead { public static void main( String args[] ) { int n = 40; // The line number · String line; try (BufferedReader br = new BufferedReader(new FileReader("file.txt"))) { for (int i = 0; i < n; i++) br.readLine(); line = br.readLine(); System.out.println(line); } catch(IOException e){ System.out.println(e); } } } Run ·
🌐
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.
🌐
HowToDoInJava
howtodoinjava.com › home › java 8 › java read a file line by line
Java Read a File Line by Line
August 22, 2023 - try { List<String> lines = FileUtils.readLines(file, Charset.defaultCharset()); } catch (IOException e) { e.printStackTrace(); } That’s all for Java example to read a file line by line. Please put your questions in the comments section. Happy Learning !! Source Code on Github ·
🌐
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 readLine() method reads a line of text from the file and returns a string containing the contents of the line, excluding any line-termination characters or null.
🌐
Funnel Garden
funnelgarden.com › java_read_file
How to Read Text and Binary Files in Java (ULTIMATE GUIDE)
The new Java I/O libraries (java.nio) had the best overall winner (java.nio.Files.readAllBytes()) but it was followed closely behind by BufferedReader.readLine() which was also a proven top performer across the board. The other excellent performer was java.nio.Files.lines(Path) which had slightly worse numbers for smaller test files but really excelled with the larger test files.
🌐
Mkyong
mkyong.com › home › java › java – read a text file line by line
Java - Read a text file line by line - Mkyong.com
December 27, 2016 - package com.mkyong; import org.apache.commons.io.FileUtils; import java.io.File; import java.io.IOException; import java.util.List; public class ReadTextFile { public static void main(String[] args) throws IOException { try { File f = new File("src/com/mkyong/data.txt"); System.out.println("Reading files using Apache IO:"); List<String> lines = FileUtils.readLines(f, "UTF-8"); for (String line : lines) { System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } The output on executing above code will be: Reading files using Apache IO: A B C D 1 2 3 ·
🌐
iO Flood
ioflood.com › blog › java-read-line
Java readLine() Method: Proper Usage Guide
February 20, 2024 - The readLine() method is then used to read a line from the file, which is printed to the console. This is a basic way to read a line in Java, but there’s much more to learn about handling different sources and advanced techniques.