Skip to content
Tutorials Center
My learning

Topics

Java Reference ยท I/O and Files

Files.readString() and readAllLines()

Reference for reading files with java.nio.file.Files: readString, readAllLines, readAllBytes, lines and newBufferedReader, with charsets and exceptions.

The Files class has one-call methods for reading a whole file. Files.readString(path) (Java 11+) returns the entire contents as one String, Files.readAllLines(path) returns a List<String> with one element per line, and Files.readAllBytes(path) returns the raw byte[]. They open the file, read it, and close it for you.

These methods load everything into memory, which is perfect for small and medium files and wrong for huge ones. For large files use Files.lines(path) (a lazy Stream<String> you must close) or BufferedReader.

Java
String text = Files.readString(Path.of("notes.txt"));
List<String> lines = Files.readAllLines(Path.of("notes.txt"));

Try it

Java readString, readAllLines and readAllBytes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Java Lazy lines() and missing files

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with IO.readln() or a Scanner), it gets the next line from this box.

Syntax

Java
static String readString(Path path) throws IOException
static String readString(Path path, Charset cs) throws IOException
static List<String> readAllLines(Path path) throws IOException
static List<String> readAllLines(Path path, Charset cs) throws IOException
static byte[] readAllBytes(Path path) throws IOException
static Stream<String> lines(Path path) throws IOException
static BufferedReader newBufferedReader(Path path) throws IOException
Name Description
path The file to read.
cs The charset used to decode bytes. Defaults to UTF-8 in all of these methods.

Returns: the text, the list of lines (without line terminators), the bytes, or a lazy stream of lines.

Throws: NoSuchFileException if the file is missing, AccessDeniedException if it cannot be read, MalformedInputException if the bytes are not valid in the charset, and OutOfMemoryError for a file that is too large (readAllBytes also fails above about 2 GB).

Usage notes

  • UTF-8 by default. Unlike FileReader before Java 18, these methods never depend on the platform default charset. See charsets.
  • Malformed input is an error, not a replacement character: reading a Latin-1 file as UTF-8 throws MalformedInputException. Pass the right charset.
  • Line terminators. readAllLines splits on \n, \r and \r\n and removes them; a trailing terminator does not produce an empty last element, but empty lines in the middle are kept.
  • Always close Files.lines. The stream holds an open file; use try-with-resources. An UncheckedIOException can be thrown while the stream is being consumed.
  • readString keeps everything, including a byte-order mark if the file has one.
  • readAllLines is a mutable ArrayList, so you can modify the returned list.
  • Memory: the file is held in memory (twice, briefly, for readString). For gigabytes, stream.
  • Resource files inside a jar are not Paths you can read this way; use Class.getResourceAsStream.