Java Files readAllLines() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
Java 7+

Definition

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.

Syntax

Path, and optionally a charset:

Java
List<String> lines = Files.readAllLines(path);                          // Java 8, UTF-8
List<String> same = Files.readAllLines(path, StandardCharsets.UTF_8);    // Java 7
CallWhat it does
Files.readAllLines(p)All lines, UTF-8.
Files.readAllLines(p, cs)All lines in charset cs.
Files.lines(p)Lazy Stream of lines for big files.

Since Java 7 (charset overload) and Java 8 (UTF-8 overload). A final line break does not create an extra empty element.

Library declarations
Java
public static List<String> readAllLines(Path path) throws IOException
public static List<String> readAllLines(Path path, Charset cs) throws IOException

Parameters

ParameterDescription
pathThe text file to read.
cs(Java 7 overload) Charset used to decode the bytes.

Return Value

Return ValueDescription
List<String>The lines in file order, without terminators. An empty file gives an empty list.

Compare with Similar Methods

Reading text, compared:

MethodHow it differs
lines()Lazy stream; must be closed; low memory.
readString()Java 11; one string with breaks kept.
newBufferedReader()Read line by line with full control.

Java Examples

Example 1 — Read a Packing List and Number Each Line

Write three lines, read them back as a list, and print them with line numbers.

Java
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import java.util.List;

public class ReadAllLinesBasic {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path packing = dir.resolve("packing.txt");
        Files.write(packing, Arrays.asList("passport", "charger", "socks"));
        List<String> lines = Files.readAllLines(packing);
        System.out.println("Lines read: " + lines.size());
        for (int i = 0; i < lines.size(); i++) {
            System.out.println((i + 1) + ". " + lines.get(i));
        }
        Files.delete(packing);
        Files.delete(dir);
    }
}

How It Works

  1. Written with system line breaks. Files.write(path, list) ends each element with the platform line separator, \r\n on Windows and \n elsewhere.
  2. Read back without them. readAllLines strips either kind of terminator, so the list holds exactly the three words on every system.
  3. A normal list. size() and get(i) work as usual, and the loop adds 1 to print human-friendly numbers.

Example 2 — Windows, Unix, and Old Mac Line Endings Together

One file mixes \r\n, \n, and \r; another ends with a line break.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;

public class ReadAllLinesEndings {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path mixed = dir.resolve("mixed.txt");
        Path trailing = dir.resolve("trailing.txt");
        Files.write(mixed, "a\r\nb\nc\rd".getBytes(StandardCharsets.UTF_8));
        Files.write(trailing, "x\ny\n".getBytes(StandardCharsets.UTF_8));
        System.out.println("mixed: " + Files.readAllLines(mixed));
        System.out.println("trailing: " + Files.readAllLines(trailing));
        Files.delete(mixed);
        Files.delete(trailing);
        Files.delete(dir);
    }
}

How It Works

  1. Exact bytes. Writing a byte array, not a list, puts precisely these terminators into the files.
  2. All three styles count. \r\n, \n, and a lone \r each end a line, so mixed.txt gives four lines.
  3. 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);
    }
}

How It Works

  1. Java 7 overload. Passing StandardCharsets.UTF_8 explicitly also compiles on Java 7.
  2. Skip the header. subList(1, size) is a view of every line after the first, so the column names are not parsed as numbers.
  3. 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.

Next: Files.readAttributes()

Read many file attributes in one call, as a typed object or a Map.

readAttributes() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful