Introduction
In this article, you will learn about different ways to use Java to read the contents of a file line-by-line. This article uses methods from the following Java classes: java.io.BufferedReader
, java.util.Scanner
, Files.readAllLines()
, and java.io.RandomAccessFile
.
Reading a File Line-by-Line using BufferedReader
You can use the readLine()
method from java.io.BufferedReader
to read a file line-by-line to String. This method returns null
when the end of the file is reached.
Here is an example program to read a file line-by-line with BufferedReader
:
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();
}
}
}
Continue your learning with the BufferedReader
API Doc (Java SE 8).
Reading a File Line-by-Line using Scanner
You can use the Scanner
class to open a file and then read its content line-by-line.
Here is an example program to read a file line-by-line with Scanner
:
package com.journaldev.readfileslinebyline;
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class ReadFileLineByLineUsingScanner {
public static void main(String[] args) {
try {
Scanner scanner = new Scanner(new File("sample.txt"));
while (scanner.hasNextLine()) {
System.out.println(scanner.nextLine());
}
scanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
Continue your learning with the Scanner
API Doc (Java SE 8).
Reading a File Line-by-Line using Files
java.nio.file.Files
is a utility class that contains various useful methods. The readAllLines()
method can be used to read all the file lines into a list of strings.
Here is an example program to read a file line-by-line with Files
:
package com.journaldev.readfileslinebyline;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
public class ReadFileLineByLineUsingFiles {
public static void main(String[] args) {
try {
List<String> allLines = Files.readAllLines(Paths.get("sample.txt"));
for (String line : allLines) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
Continue your learning with the Files
API Doc (Java SE 8).
Reading a File Line-by-Line using RandomAccessFile
You can use RandomAccessFile
to open a file in read mode and then use its readLine
method to read a file line-by-line.
Here is an example program to read a file line-by-line with RandomAccessFile
:
package com.journaldev.readfileslinebyline;
import java.io.IOException;
import java.io.RandomAccessFile;
public class ReadFileLineByLineUsingRandomAccessFile {
public static void main(String[] args) {
try {
RandomAccessFile file = new RandomAccessFile("sample.txt", "r");
String str;
while ((str = file.readLine()) != null) {
System.out.println(str);
}
file.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Continue your learning with the RandomAccessFile
API Doc (Java SE 8).
Conclusion
In this article, you learned about different ways to use Java to read the contents of a file line-by-line.
Continue your learning with more Java tutorials.
Source:
https://www.digitalocean.com/community/tutorials/java-read-file-line-by-line