In order to read a text file line by line using Java code you can make use of multiple options, let us see 3 examples,
Sample text file sample.txtLine 1
Line 2
Line 3
Line 4
Line 5
Example 1: Using BufferedReader and FileReader from java.io package,
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class JavaReadTextFile {
public static void main(String[] args) throws IOException {
String fileName = "/Users/proj/src/main/sample.txt";
FileReader file = new FileReader(fileName);
BufferedReader bufferedReader = new BufferedReader(file);
String line;
while ((line = bufferedReader.readLine()) != null) {
System.out.println(line);
}
}
}
Example 2: Using Java 7 Files from java.nio package,
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class JavaReadTextFile {
public static void main(String[] args) throws IOException {
String fileName = "/Users/proj/src/main/sample.txt";
Path path = Paths.get(fileName);
Files.lines(path).forEach(System.out::println);
}
}
Example 3: Using Java 8 Streams and Files
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.stream.Stream;
public class JavaReadTextFile {
public static void main(String[] args) throws IOException {
String fileName = "/Users/proj/src/main/sample.txt";
Stream<String> linesStream = Files.lines(Paths.get(fileName));
linesStream.forEach(System.out::println);
}
}

-
Have Questions? Post them here!
More Posts related to Java,
- Add two numbers using Java Generics
- Convert Java List to Json String using Jackson
- Convert Java Object to JSON using Jackson Library
- Java SE JDBC: Insert with PreparedStatement Example
- [Program] How to read three different values using Scanner in Java
- Java JDBC Batch Update Example with PreparedStatement
- Java Stream flatmap() Examples
- Save Java Object as JSON file using Jackson Library
- Java get day of the week as an int using DayOfWeek
- Create Nested Directories using Java Code
- Java JDBC Delete a Record in Database Table using PreparedStatement
- List of jars required for Struts2 project
- Convert Java Object to XML using Jackson Library
- Struts2 : java.lang.ClassNotFoundException: org.apache.commons.fileupload.RequestContext
- Java JDBC Get Id of the Inserted Record with AutoIncrement
- How to list all tables using Java JDBC
- Java Jackson ObjectMapper Class with Examples
- Fix: Maven - Failed to execute goal - Compilation failure - Source/Target option 5 is no longer supported. Use 7 or later
- Eclipse : The type java.lang.CharSequence cannot be resolved. Indirectly referenced from required .class files
- Formatting Double in Java [Examples]
- How to run Java Unit Test cases with Apache Maven?
- [fix] NullPointerException Cannot Invoke findById because Repository is null - Java Spring
- [Fix] java: integer number too large compilation error
- [Java] Read a File with UTF-8 Encoding
- How to detect Operating System using Java code
More Posts:
- Error: Failed to validate the signature of the actionable message card - Power Automate Flow - PowerAutomate
- Float built-in function in Python - Python
- How to change user image icon macOS Big Sur - MacOS
- How to add two float numbers in Python - Python
- How to open terminal on Mac to run commands - MacOS
- Notepad++ Happy vs Unhappy Versions - NotepadPlusPlus
- How to take user input from the console in a Python program - Python
- How to change Android Titlebar theme color from Purple - Android