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 Top answer 1 of 5
74
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());
2 of 5
32
Use BufferedReader and InputStreamReader classes.
BufferedReader buffer=new BufferedReader(new InputStreamReader(System.in));
String line=buffer.readLine();
Or use java.util.Scanner class methods.
Scanner scan=new Scanner(System.in);
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(); } } }
Videos
06:54
How to READ FILES with Java in 8 minutes! 📖 - YouTube
04:46
How to Read All Lines from file in Java - YouTube
03:24
Java Practice Examples - Module 7-2 Reading Multiple Lines from ...
How to Easily Read and Write to Files in Java (File I/O) - Java ...
07:25
Read File Line By Line Using Buffered Streams in Java | Ameerpet ...
06:26
Reading a SPECIFIC Line in a File | Java - YouTube
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.
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 ·
Reddit
reddit.com › r/learnjava › read lines from file using java 8 - linereader or streams?
r/learnjava on Reddit: Read lines from file using Java 8 - LineReader or Streams?
May 12, 2015 -
I wonder what is better for getting the lines of a file (and later writing them back into a new file, using PrintWriter atm).
Anyone can enlighten me? :)
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 ·