Java Read Files

Su Suriya Ravichandran Updated 18 Nov 2026
10 min read ·Lesson 136 of 306

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:

MethodReturnsLoads the whole file into memory?Best for
Files.readString(path)One StringYesSmall text files: templates, JSON, configuration
Files.readAllLines(path)List<String>, one element per lineYesSmall line-based files you need to access by index
Files.lines(path)Stream<String>, read lazilyNoLarge line-based files, filtering and aggregating
Files.newBufferedReader(path)BufferedReader, read line by lineNoLarge files, full control over the loop
new Scanner(path)Scanner, reads tokensNoWhitespace-separated values, mixed numbers and words
Files.readAllBytes(path)byte[]YesSmall 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 the Path), so it runs on its own.
  • Files.readString(file) (Java 11+) returns the entire content, including the newline characters, as one String. The 60 characters include the four \n characters.
  • 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:

  1. Files.newBufferedReader(file) opens the file and returns a BufferedReader that 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.
  2. reader.readLine() returns the next line without its terminator, or null at the end of the file.
  3. while ((line = reader.readLine()) != null) reads a line, assigns it to line, and compares the assigned value with null — all in the condition. The inner parentheses are required: without them, != would be evaluated first.
  4. 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 an OptionalDouble, empty if no rows matched, so orElse(0) supplies a default.
  • The stream must be closed. Unlike most streams, the one returned by Files.lines holds an open file handle. Forgetting try-with-resources leaks the handle — a common bug in code that calls Files.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 as UncheckedIOException, 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; pass StandardCharsets.UTF_8 as 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 a double without consuming it; nextDouble() then consumes and converts it. Calling nextDouble() on "abc" would throw InputMismatchException, so the non-numeric token is consumed with next() instead.
  • hasNextDouble and nextDouble use the default locale to parse numbers. In a locale that uses a comma as the decimal separator (such as German), 23.5 wouldn't be recognised. For files with a fixed format, set the locale explicitly: sc.useLocale(Locale.ROOT).
  • Scanner is considerably slower than BufferedReader because 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:

  1. 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.
  2. Files.readString(legacy) decodes as UTF-8, where a byte like E9 must be followed by specific continuation bytes. They aren't there, so the decoder throws MalformedInputException (a subclass of IOException). The message reports the length of the invalid byte sequence it found, not its position.
  3. 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:

  1. 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 empty Optional instead of throwing an exception, so one unexpected line doesn't abort the analysis.
  2. flatMap(Optional::stream) turns each Optional<Entry> into a stream of zero or one elements, which drops the unparseable lines and unwraps the valid ones.
  3. 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.)
  4. groupingBy(Entry::level, TreeMap::new, counting()) counts entries per level; the TreeMap keeps the keys sorted alphabetically, which makes the output stable.
  5. Repeated errors are grouped by message, using a LinkedHashMap to 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.

0 Comments

Reviewed before they appear

No comments yet.

Java
Java Introduction Java Features Java Syntax Java Comments Java Statements Java Output Java Variables Java Data Types Java Type Casting Java Operators Java Strings Java Numbers Java Math Java Booleans Java User Input Java Conditions Java If Else Java Switch Java While Loop Java For Loop Java Break Java Continue Java Arrays Java Methods Java Parameters Java Return Values Java Scope Java Recursion Install Java Install JDK Java IDEs Java JDK Java JRE Java JVM Java Compiler Java File Compile Java Run Java JShell Basics Java Packages Java OOP Java Classes Java Objects Class Methods Constructors Constructor Overload this Keyword static Keyword final Keyword Encapsulation Inheritance Polymorphism Abstraction Access Modifiers super Keyword Method Overload Method Override Instanceof Operator Nested Classes Inner Classes Anonymous Classes Enums Records Array Basics Array Length Loop Arrays Multidimensional Arrays Array Copy Array Sorting Array Search ArrayList LinkedList HashSet TreeSet HashMap TreeMap Queue Interface Deque Interface Stack Class Iterator List Interface Set Interface Map Interface Collections Class Comparable Comparator Generics Basics Generic Classes Generic Methods Wildcards String Basics String Length String Methods String Concatenation String Compare String Search String Replace String Split String Format StringBuilder StringBuffer Text Blocks Regular Expressions Number Types Integer Class Float Class Double Class BigInteger BigDecimal Math Class Random Numbers Number Format Date Basics LocalDate LocalTime LocalDateTime ZonedDateTime Date Formatting Date Parsing Period Class Duration Class Java Instant Class Java Exception Basics Java try...catch Java Multiple Catch Blocks Java finally Block Java throw Keyword Java throws Keyword Java Checked Exceptions Java Unchecked Exceptions Java Custom Exceptions Java try-with-resources Java Exception Chaining Java File Basics Java Create Files Java Read Files Java Write Files Java Delete Files Java File Paths Java File Class Java BufferedReader Java BufferedWriter Java FileInputStream Java FileOutputStream Java Object Streams Java Directory Handling Java NIO Basics Java Files API Java Input Java Scanner Class Java Console Input Java PrintStream Java InputStream Java OutputStream Java Reader Class Java Writer Class Java Buffered Streams Java Data Streams Java Generics Introduction Java Generic Types Java Generic Methods Java Bounded Types Java Wildcards Java Type Erasure Java Generic Interfaces Java Generic Collections Java Lambda Basics Java Lambda Syntax Java Functional Interfaces Java Method References Java Stream API Java Stream Creation Java Stream Filter Java Stream Map Java Stream Reduce Java Stream Sort Java Stream Collect Java Collectors Class Java Optional Class Java Parallel Streams Java Threads: Thread Basics Create a Thread in Java Java Thread Class Java Runnable Interface Java Thread States Java Thread Sleep Java Thread Join Java Thread Priority Java Synchronization Java Thread Safety Java Race Conditions Java Deadlocks Java Executor Service Java Thread Pools Java Callable Interface Java Future Interface Java CompletableFuture Java Concurrent Collections Java Atomic Classes Java Virtual Threads Java Networking Basics Java URL Class Java URI Class Java HttpClient Java HTTP Requests Java HTTP Responses Java Socket Basics Java ServerSocket Java DatagramSocket Java WebSocket Basics Java JDBC Basics Java JDBC Setup Java Database Connection Java JDBC Connection Interface Java JDBC Statement Java PreparedStatement Java ResultSet Java JDBC CRUD Operations Java JDBC Transactions Java JDBC Batch Update Java Connection Pooling with HikariCP Java JPA Basics: Entities, EntityManager and JPQL Java Hibernate Basics: SessionFactory, Session and HQL Package in Java: Basics Explained with Examples Create Package in Java: Step-by-Step Guide with Examples Import Package in Java: The import Statement Explained Access Control in Java: Packages, Classes and Members JAR File in Java: Create, Run and Inspect JAR Files Java Modules: Introduction to the Java Module System module-info in Java: Every Module Directive Explained Java Module Path: How --module-path Finds Your Modules Java Classpath: How -cp and CLASSPATH Find Your Classes Java Annotations: Basics of @ Metadata in Java Java Built-in Annotations: @Override, @Deprecated and More Java Custom Annotations: Create Your Own @interface Java Annotation Targets: @Target and ElementType Explained Java Reflection API: Inspect and Use Classes at Runtime Java Class Metadata: Names, Modifiers, Hierarchy and Generic Java Dynamic Objects: Create Objects Dynamically With Reflec Java Dynamic Proxy: Intercept Method Calls With Proxy and In Java Security Java Secure Coding Java Password Hashing Java Input Validation Java Cryptography Basics Java KeyStore Basics Java TLS Basics Java SecureRandom Java JUnit Basics Java JUnit Test Methods JUnit Assertions JUnit Test Fixtures JUnit Parameterized Tests Mockito in Java Java Mock Objects Java Integration Testing Java Code Coverage Maven in Java Create a Maven Project in Java Maven Dependencies Maven Plugins Gradle in Java Gradle Build Gradle Dependencies Build Executable JAR in Java Build WAR File in Java HTTP Basics in Java Java Servlet Basics Servlet Lifecycle in Java HttpServletRequest in Java HttpServletResponse in Java JSP in Java JSTL in JSP REST API in Java JSON in Java Java Web Application Spring Framework Basics Spring Boot Tutorial Spring MVC Spring Data JPA Spring Security Spring Boot REST API Jakarta EE Basics Java Deployment JAR Deployment Java WAR Deployment Run Java as a Service Docker Java: Dockerize a Java Application Java Environment Variables and Configuration Java Logging Basics Java Production Setup Java JVM Options Java JVM Monitoring Java Hello World App Java Calculator App Java Number Guess Game Java Student Manager Java Library System Java Bank App Java Todo App Java File Manager Java Quiz App Java REST API Project Java Chat Server Java Employee Manager Java E-commerce API
Ask about this post
AI Ask about this post

Ask questions about Java Read Files and get answers drawn from it.

Signed-in readers only.