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
🌐
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); } }
🌐
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(); } } }
🌐
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(); } } }
🌐
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.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bufferedreader-readline-method-in-java-with-examples
BufferedReader readLine() method in Java with Examples - GeeksforGeeks
May 28, 2020 - // Java program to illustrate // BufferedReader readLine() method import java.io.*; public class GFG { public static void main(String[] args) { // Read the stream 'demo.txt' // containing text // "GEEKSFORGEEKS" // "geeksforgeeks" FileReader fileReader = new FileReader( "c:/demo.txt"); // Convert fileReader to // bufferedReader BufferedReader buffReader = new BufferedReader( fileReader); while (buffReader.ready()) { System.out.println( buffReader.readLine()); } } }
Find elsewhere
🌐
Reddit
reddit.com › r/learnjava › question about bufferedreader: read / readline
r/learnjava on Reddit: Question about BufferedReader: read / readLine
March 10, 2021 -

Say I am given this condition in a loop:

While ((i = br.readLine()) != null)

How does the system know to keep going on the scan? I’m not asking about the null- I’m aware it terminates once the file reaches a null line, but how does readLine() keep incrementing the line which is being read?

For a loop traversal, you would need a for loop with i++ for example... I don’t see any incrementing going on?

Second question: how can I look for a specific string in a file? == “” didn’t work.

🌐
W3Schools
w3schools.com › java › java_bufferedreader.asp
Java BufferedReader
Java gives you several ways to read files. Here's when to pick each one: Scanner - best for simple text. It can split text into lines, words, or numbers (e.g., nextInt(), nextLine()). BufferedReader - best for large text files. It is faster, uses less memory, and can read full lines with readLine().
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.io.bufferedreader.readline
BufferedReader.ReadLine Method (Java.IO) | Microsoft Learn
Reads a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), a carriage return followed immediately by a line feed, or by reaching the end-of-file (EOF).
🌐
HackerRank
candidatesupport.hackerrank.com › articles › 2201684846-execution-environment
Execution Environment | Hackerrank Candidates Knowledge Base
April 22, 2026 - HackerRank provides a comprehensive execution environment that supports multiple programming languages and frameworks, allowing you to run and
🌐
Tutorialspoint
tutorialspoint.com › java › io › bufferedreader_readline.htm
Java - BufferedReader readLine() method
The Java BufferedReader readLine() method read 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.
🌐
W3Schools
w3schools.com › java › java_files_read.asp
Java Read Files
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
🌐
EDUCBA
educba.com › home › software development › software development tutorials › java tutorial › java bufferedreader
Java BufferedReader | How Java BufferedReader Class works? Example
April 10, 2023 - 6. String readLine() Method of Java BufferedReader Class: The string readLine () method will read the text line / lines as needed.
Address   Unit no. 202, Jay Antariksh Bldg, Makwana Road, Marol, Andheri (East),, 400059, Mumbai
🌐
Apple Community
discussions.apple.com › thread › 3295304
What's wrong with Java readline()? - Apple Community
August 29, 2011 - I just updated to OS X 10.6.8 and installed the Java update 1.6.0_26-b03-384. It appears that BufferedReader.readLine() is broken for reading input lines from the console. readLine() does not terminate with a newline. This is pretty basic. What's up? import java.io.*; /** This class demonstrates how to read a line of text from the keyboard ·
🌐
GeeksforGeeks
geeksforgeeks.org › java › ways-to-read-input-from-console-in-java
Ways to Read Input from Console in Java - GeeksforGeeks
January 16, 2026 - import java.io.*; public class Geeks { public static void main(String[] args) throws IOException { DataInputStream r = new DataInputStream(System.in); System.out.print("Enter an integer: "); int i = Integer.parseInt(r.readLine()); System.out.print("Enter a string: "); String s = r.readLine(); System.out.println("You entered integer: " + i); System.out.println("You entered string: " + s); } }
🌐
Career Ride
careerride.com › Java-QA-how-to-read-line-of-input.aspx
Explain how to read a line of input at a time
Input from the console is read from the System.in object and is then wrapped by an InputStreamReader which reads one character at a time......
🌐
TheServerSide
theserverside.com › blog › Coffee-Talk-Java-News-Stories-and-Opinions › User-input-with-the-Java-Console-class
User input with Java's Console class
Introduced in Java 6, Java’s ... ... The readLine() method takes user input through the console window, stores the input data in a String, and echoes the input back to the user as he or she types....
🌐
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.
🌐
IPRoyal
iproyal.com
IPRoyal | Premium Quality Proxies, Unbeatable Prices
import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class ApiRequest { public static void main(String[] args) { String apiToken = "<your_api_token>"; String urlString = "https://resi-api.iproyal.com/v1/me"; try { URL url = new URL(urlString); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setRequestProperty("Authorization", "Bearer " + apiToken); int responseCode = connection.getResponseCode(); if (responseCode == HttpURLConnection.HTTP_OK) { Buff