Read a text file line-by-line in Java
This post will discuss how to read a text file line-by-line in Java.
1. Using BufferedReader class
A simple solution to read a text file line-by-line is using the BufferedReader whose readLine() method can read a line of text. Following is a simple example demonstrating the usage of this method, where each invocation of the readLine() method would read the next line from the file and return it as a string.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) { File file = new File("/home/data/file.txt"); try { try (BufferedReader br = new BufferedReader(new FileReader(file))) { String line; while ((line = br.readLine()) != null) { System.out.println(line); } } } catch (IOException e) { e.printStackTrace(); } } } |
Starting with Java 8, you can use the lines() method that returns a stream of lines of text read from the BufferedReader. This is demonstrated below:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; public class Main { public static void main(String[] args) { File file = new File("/home/data/file.txt"); try { try (BufferedReader br = new BufferedReader(new FileReader(file))) { br.lines().forEach(System.out::println); } } catch (IOException e) { e.printStackTrace(); } } } |
2. Using Files class
The Files class consists of several static methods that operate on files. Since JDK 1.8, you can use the static method Files.lines() to get all lines from the file as a Stream. Even though this method returns a Stream, you should not use chaining as we do with other Stream operations. To ensure that the stream is closed promptly after completion of the stream’s operations, you must enclose this method within a try-with-resources statement.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.stream.Stream; public class Main { public static void main(String[] args) { String fileName = "/home/data/file.txt"; try { try (Stream<String> stream = Files.lines(Paths.get(fileName))) { stream.forEach(System.out::println); } } catch (IOException e) { e.printStackTrace(); } } } |
Alternatively, to get a list of all lines from a file, you can directly use the Files.readAllLines() method. This elegant and concise method is introduced in Java SE 8.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; import java.util.List; public class Main { public static void main(String[] args) { String fileName = "/home/data/file.txt"; try { List<String> lines = Files.readAllLines(Paths.get(fileName)); System.out.println(lines); } catch (IOException e) { e.printStackTrace(); } } } |
3. Using Scanner class
Another option is to use the Scanner class to read each line of the file with the nextLine() method. Following is a simple example demonstrating the usage of this method. Note that the hasNextLine() method returns true only if the scanner has more lines left, and the nextLine() method advances the scanner to the next line and returns the current line.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import java.io.File; import java.io.IOException; import java.util.Scanner; public class Main { public static void main(String[] args) { String fileName = "/home/data/file.txt"; try { Scanner sc = new Scanner(new File(fileName)); while (sc.hasNextLine()) { String line = sc.nextLine(); System.out.println(line); } } catch (IOException e) { e.printStackTrace(); } } } |
4. Using IOUtils class
If you prefer Apache Commons IO library, you can use the readLines() method from the IOUtils class. It returns the contents of a Reader as a list of strings, one entry per line.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
import org.apache.commons.io.IOUtils; import java.io.FileReader; import java.io.IOException; import java.util.List; public class Main { public static void main(String[] args) { String fileName = "/home/data/file.txt"; try { List<String> lines = IOUtils.readLines(new FileReader(fileName)); System.out.println(lines); } catch (IOException e) { e.printStackTrace(); } } } |
That’s all about reading a text file line-by-line in Java.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)