Java Read Files
Choosing a Way to Read a File
Java has several ways to read a file. They differ in how much of the file they load into memory and what shape the data comes back in:
| Method | Returns | Loads the whole file into memory? | Best for |
|---|---|---|---|
Files.readString(path) | One String | Yes | Small text files: templates, JSON, configuration |
Files.readAllLines(path) | List<String>, one element per line | Yes | Small line-based files you need to access by index |
Files.lines(path) | Stream<String>, read lazily | No | Large line-based files, filtering and aggregating |
Files.newBufferedReader(path) | BufferedReader, read line by line | No | Large files, full control over the loop |
new Scanner(path) | Scanner, reads tokens | No | Whitespace-separated values, mixed numbers and words |
Files.readAllBytes(path) | byte[] | Yes | Small binary files: images, keys, checksums |
The general rule: if the file is small (a few megabytes at most), the "read everything" methods are the simplest. If the file can be large — logs, data exports, uploaded CSVs — read it incrementally so memory use stays constant regardless of file size.
All the examples below use this file, students.csv:
name,course,score
Asha,Java,91
Ravi,Python,78
Meena,Java,85
Reading the Whole File: readString and readAllLines
import java.io.IOException;
import java.nio.file.*;
import java.util.List;
public class Read1 {
public static void main(String[] args) throws IOException {
Path file = Files.writeString(Path.of("students.csv"),
"name,course,score\nAsha,Java,91\nRavi,Python,78\nMeena,Java,85\n");
String all = Files.readString(file);
System.out.println("characters: " + all.length());
List<String> lines = Files.readAllLines(file);
System.out.println("lines: " + lines.size() + ", header: " + lines.get(0));
for (String line : lines.subList(1, lines.size())) {
String[] f = line.split(",");
System.out.println(f[0] + " scored " + f[2] + " in " + f[1]);
}
}
}
Output:
characters: 60
lines: 4, header: name,course,score
Asha scored 91 in Java
Ravi scored 78 in Python
Meena scored 85 in Java
How it works:
- The program first writes the sample file with
Files.writeString(which returns thePath), so it runs on its own. Files.readString(file)(Java 11+) returns the entire content, including the newline characters, as oneString. The 60 characters include the four\ncharacters.Files.readAllLines(file)splits the content into lines and removes the line terminators. A trailing newline at the end of the file doesn't produce an extra empty element, so the list has 4 elements.lines.subList(1, lines.size())skips the header row.split(",")then separates the columns.
Both methods open, read and close the file internally, so no try-with-resources is needed, and both decode the bytes as UTF-8 unless you pass a different charset. Their limitation is memory: a 2 GB log file read with readAllLines needs several gigabytes of heap, because each line becomes a separate String object, and usually ends in OutOfMemoryError.
Reading Line by Line: BufferedReader
import java.io.*;
import java.nio.file.*;
public class Read2 {
public static void main(String[] args) throws IOException {
Path file = Path.of("students.csv");
try (BufferedReader reader = Files.newBufferedReader(file)) {
String line;
int number = 0;
while ((line = reader.readLine()) != null) {
number++;
System.out.printf("%2d | %s%n", number, line);
}
}
}
}
Output:
1 | name,course,score
2 | Asha,Java,91
3 | Ravi,Python,78
4 | Meena,Java,85
This is the classic loop for reading text files of any size:
Files.newBufferedReader(file)opens the file and returns aBufferedReaderthat decodes UTF-8. It reads the file from disk in blocks (8,192 characters by default) and hands out lines from that buffer, which is far faster than reading one character at a time.reader.readLine()returns the next line without its terminator, ornullat the end of the file.while ((line = reader.readLine()) != null)reads a line, assigns it toline, and compares the assigned value withnull— all in the condition. The inner parentheses are required: without them,!=would be evaluated first.- try-with-resources closes the file when the loop ends or if an exception occurs. Only one line is held in memory at a time, so this loop handles a 10 GB file as easily as a 1 KB one.
BufferedReader and the reader classes are covered in depth in the BufferedReader and Reader Class lessons.
Reading as a Stream: Files.lines
Files.lines returns the lines as a lazily populated Stream<String>, which combines incremental reading with the Stream API's filtering and aggregation operations.
import java.io.IOException;
import java.nio.file.*;
import java.util.stream.Stream;
public class Read3 {
public static void main(String[] args) throws IOException {
Path file = Path.of("students.csv");
try (Stream<String> lines = Files.lines(file)) {
double avg = lines.skip(1)
.map(l -> l.split(","))
.filter(f -> f[1].equals("Java"))
.mapToInt(f -> Integer.parseInt(f[2]))
.average()
.orElse(0);
System.out.println("Java average: " + avg);
}
}
}
Output:
Java average: 88.0
- Lazy reading. Lines are read from the file only as the stream pipeline requests them, so memory use stays small even for huge files.
- The pipeline: skip the header, split each line into fields, keep only Java rows, convert the score to
int, and average them:(91 + 85) / 2 = 88.0.average()returns anOptionalDouble, empty if no rows matched, soorElse(0)supplies a default. - The stream must be closed. Unlike most streams, the one returned by
Files.linesholds an open file handle. Forgetting try-with-resources leaks the handle — a common bug in code that callsFiles.lines(path).count()inline. - I/O errors become unchecked. Opening the file can throw
IOException, but an error that happens while the stream is being consumed (for example, invalid UTF-8 on line 50,000) is thrown asUncheckedIOException, because stream operations can't throw checked exceptions.
Reading Tokens: Scanner
Scanner splits input into tokens separated by whitespace (spaces, tabs, newlines) and can convert them to numbers. It's convenient for files of numbers or words rather than structured lines.
import java.io.IOException;
import java.nio.file.*;
import java.util.Scanner;
public class Read4 {
public static void main(String[] args) throws IOException {
Path file = Files.writeString(Path.of("readings.txt"), "23.5 24.1\n25.0\n 22.8 abc 26.2\n");
double sum = 0;
int count = 0;
try (Scanner sc = new Scanner(file)) {
while (sc.hasNext()) {
if (sc.hasNextDouble()) {
sum += sc.nextDouble();
count++;
} else {
System.out.println("skipping token: " + sc.next());
}
}
}
System.out.printf("%d readings, average %.2f%n", count, sum / count);
}
}
Output:
skipping token: abc
5 readings, average 24.32
new Scanner(Path)decodes the file with the platform default charset (UTF-8 since Java 18; passStandardCharsets.UTF_8as a second argument for older versions) and ignores line structure: values on the same line and on different lines are all just tokens.hasNextDouble()checks whether the next token can be parsed as adoublewithout consuming it;nextDouble()then consumes and converts it. CallingnextDouble()on"abc"would throwInputMismatchException, so the non-numeric token is consumed withnext()instead.hasNextDoubleandnextDoubleuse the default locale to parse numbers. In a locale that uses a comma as the decimal separator (such as German),23.5wouldn't be recognised. For files with a fixed format, set the locale explicitly:sc.useLocale(Locale.ROOT).Scanneris considerably slower thanBufferedReaderbecause it uses regular expressions to find tokens. It's fine for small inputs but not for multi-gigabyte files. The Scanner Class lesson covers it in more detail.
Reading Bytes: readAllBytes
For binary files — images, PDFs, keys — or when you need the raw bytes of a text file, read them without decoding:
byte[] logo = Files.readAllBytes(Path.of("logo.png"));
System.out.println(logo.length + " bytes");
Like readString, this loads the whole file. For large binary files, use Files.newInputStream (FileInputStream and InputStream lessons) or Files.copy to process them in chunks.
Encoding Problems When Reading
The bytes in a file mean nothing until they're decoded with a character encoding. If the file was written in one encoding and is read with another, Java either produces wrong characters or refuses to read the file.
import java.io.IOException;
import java.nio.charset.*;
import java.nio.file.*;
public class Read5 {
public static void main(String[] args) throws IOException {
Path legacy = Path.of("legacy-customers.csv");
Files.writeString(legacy, "name,city\nJosé,Málaga\n", StandardCharsets.ISO_8859_1);
try {
Files.readString(legacy);
} catch (MalformedInputException e) {
System.out.println("MalformedInputException: " + e.getMessage());
}
System.out.print(Files.readString(legacy, StandardCharsets.ISO_8859_1));
byte[] raw = Files.readAllBytes(legacy);
System.out.println("bytes: " + raw.length);
}
}
Output:
MalformedInputException: Input length = 3
name,city
José,Málaga
bytes: 22
What happened:
- The file is written in ISO-8859-1 (Latin-1), as older systems and some spreadsheet exports do. In this encoding
éandáare single bytes (E9,E1), so the file is exactly one byte per character: 9 characters + newline + 11 characters + newline = 22 bytes. In UTF-8 the same text would take 24 bytes, becauseéandáneed two bytes each. Files.readString(legacy)decodes as UTF-8, where a byte likeE9must be followed by specific continuation bytes. They aren't there, so the decoder throwsMalformedInputException(a subclass ofIOException). The message reports the length of the invalid byte sequence it found, not its position.- Reading with the correct charset,
StandardCharsets.ISO_8859_1, decodes the text properly.
readString, readAllLines and Files.lines report malformed input with an exception, which is good: you find out immediately that the encoding is wrong. Some older APIs, such as new String(bytes) and InputStreamReader, silently replace invalid bytes with � instead, and corrupted data then flows into your database unnoticed.
A file doesn't record its own encoding, so you can't reliably detect it; it has to be agreed on with whoever produces the file. In practice: use UTF-8 for everything you create, and pass the agreed charset explicitly when reading files from other systems.
Practical Example: Analysing a Log File
A common task is to summarise a log file: how many entries of each level, and which errors keep repeating. Logs can be very large, so the example reads them with Files.lines, and it must tolerate lines that don't follow the expected format.
import java.io.IOException;
import java.nio.file.*;
import java.util.*;
import java.util.stream.*;
public class Logs {
record Entry(String time, String level, String message) { }
static Optional<Entry> parse(String line) {
String[] p = line.split(" ", 3);
if (p.length < 3 || !Set.of("INFO", "WARN", "ERROR").contains(p[1])) return Optional.empty();
return Optional.of(new Entry(p[0], p[1], p[2]));
}
public static void main(String[] args) throws IOException {
Path log = Files.writeString(Path.of("app.log"), String.join("\n",
"09:00:01 INFO server started on port 8080",
"09:00:05 INFO GET /courses 200",
"09:01:12 WARN slow query took 1840 ms",
"09:02:30 ERROR payment gateway timeout",
"--- rotated ---",
"09:03:02 INFO GET /cart 200",
"09:04:45 ERROR payment gateway timeout",
"09:05:10 ERROR database connection refused",
""));
Map<String, Long> byLevel;
List<Entry> errors;
try (Stream<String> lines = Files.lines(log)) {
List<Entry> entries = lines.map(Logs::parse).flatMap(Optional::stream).toList();
byLevel = entries.stream().collect(Collectors.groupingBy(Entry::level, TreeMap::new, Collectors.counting()));
errors = entries.stream().filter(e -> e.level().equals("ERROR")).toList();
}
System.out.println("entries by level: " + byLevel);
Map<String, Long> repeated = errors.stream()
.collect(Collectors.groupingBy(Entry::message, LinkedHashMap::new, Collectors.counting()));
repeated.forEach((msg, n) -> System.out.println(" " + n + " x " + msg));
System.out.println("last error at " + errors.get(errors.size() - 1).time());
}
}
Output:
entries by level: {ERROR=3, INFO=3, WARN=1}
2 x payment gateway timeout
1 x database connection refused
last error at 09:05:10
Step by step:
- Parsing each line.
split(" ", 3)splits into at most three parts — time, level and the rest — so spaces inside the message are kept. Lines that don't have a known level (the--- rotated ---marker) produce an emptyOptionalinstead of throwing an exception, so one unexpected line doesn't abort the analysis. flatMap(Optional::stream)turns eachOptional<Entry>into a stream of zero or one elements, which drops the unparseable lines and unwraps the valid ones.- Reading happens inside try-with-resources. The file is read once into a list of parsed entries, which are much smaller than the whole file would be in a real log because only the needed lines are kept. (For a truly huge log, you'd aggregate the counts directly in the stream instead of collecting a list first.)
groupingBy(Entry::level, TreeMap::new, counting())counts entries per level; theTreeMapkeeps the keys sorted alphabetically, which makes the output stable.- Repeated errors are grouped by message, using a
LinkedHashMapto keep them in the order they first appeared. The same timeout occurring twice is the kind of pattern an operations team wants to see first.
The structure — read lazily, parse defensively, aggregate — applies equally to CSV imports, access logs and sensor data.