In Java, Files.readAllLines(path) reads the whole file and returns a List<String> with one element per line. The line terminators (\n, \r\n, or a lone \r) are removed, so the result is the same whichever system wrote the file.
The overload without a charset (Java 8) decodes UTF-8; the Java 7 overload takes a Charset. Because all lines are held in memory at once, it suits configuration files, small data files, and tests.
A static method of the final utility class java.nio.file.Files, available since Java 7. Call it on the class name, as in Files.readAllLines(...), after import java.nio.file.Files;. You never create a Files object.
Run with java ReadAllLinesEndings. The output is the same on Windows, macOS, and Linux.
mixed: [a, b, c, d]
trailing: [x, y]
How It Works
Exact bytes. Writing a byte array, not a list, puts precisely these terminators into the files.
All three styles count.\r\n, \n, and a lone \r each end a line, so mixed.txt gives four lines.
No ghost line. The final \n in trailing.txt ends the line y; it does not start an empty fourth element.
Example 3 — Add Up a Column, Skipping the Header
Read a small CSV file, skip the first line, and total the quantity column.
Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
public class ReadAllLinesCsv {
public static void main(String[] args) throws IOException {
Path dir = Files.createTempDirectory("demo");
Path csv = dir.resolve("fruit.csv");
Files.write(csv, "item,qty\napple,3\npear,5\nplum,4".getBytes(StandardCharsets.UTF_8));
List<String> lines = Files.readAllLines(csv, StandardCharsets.UTF_8);
int total = 0;
for (String line : lines.subList(1, lines.size())) {
String[] cols = line.split(",");
total += Integer.parseInt(cols[1]);
}
System.out.println("Header: " + lines.get(0));
System.out.println("Rows: " + (lines.size() - 1) + ", total qty: " + total);
Files.delete(csv);
Files.delete(dir);
}
}
Output
Run with java ReadAllLinesCsv. The output is the same on Windows, macOS, and Linux.
Header: item,qty
Rows: 3, total qty: 12
How It Works
Java 7 overload. Passing StandardCharsets.UTF_8 explicitly also compiles on Java 7.
Skip the header.subList(1, size) is a view of every line after the first, so the column names are not parsed as numbers.
Split and add. Each row splits at the comma, and the quantities 3, 5, and 4 add up to 12. Real CSV with quoted commas needs a proper parser.
Frequently Asked Questions
Call Files.readAllLines(path) on Java 8 or later, or Files.readAllLines(path, StandardCharsets.UTF_8) on Java 7.
No. \n, \r\n, and \r are recognised and removed.
The one-argument overload always uses UTF-8, not the platform default. Pass a Charset for other encodings.
The file is not valid UTF-8, usually because it was saved in a legacy encoding. Read it with the right Charset, for example StandardCharsets.ISO_8859_1.
When the file is large or you only need part of it. lines() reads lazily and keeps only the current line in memory.