Java Files size() Method

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

Definition

In Java, Files.size(path) returns the size of the file in bytes as a long, so files larger than 2 GB are no problem.

The size counts bytes, not characters. In UTF-8 a plain ASCII letter is one byte, but a character like e-acute takes two. For a folder the value is system specific and does not include its contents.

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.size(...), after import java.nio.file.Files;. You never create a Files object.

Syntax

Pass the path; it throws if the file is missing:

Java
long bytes = Files.size(dir.resolve("photo.jpg"));
double kb = bytes / 1024.0;
CallWhat it does
Files.size(p)Size in bytes.
Files.readAttributes(p, BasicFileAttributes.class).size()Same value, read together with other attributes.
p.toFile().length()Old way; returns 0 for missing files.

Since Java 7. Unlike File.length(), which returns 0 when the file does not exist, Files.size throws, so an empty file and a missing file are never confused.

Library declarations
Java
public static long size(Path path) throws IOException

Parameters

ParameterDescription
pathThe file to measure. Symbolic links are followed.

Return Value

Return ValueDescription
longThe file size in bytes.

Compare with Similar Methods

Measuring data:

MethodHow it differs
readAttributes()Size plus type and times in one call.
readAllBytes()The bytes themselves; length equals the size.
walk()Needed to add up a folder tree.

Java Examples

Example 1 — Five Characters, Five Bytes; Four Characters, Five Bytes

Write two words as UTF-8 and compare their size with their length in characters.

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

public class SizeBytes {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path hello = dir.resolve("hello.txt");
        Path cafe = dir.resolve("cafe.txt");
        String word = "caf\u00e9";
        Files.write(hello, "Hello".getBytes(StandardCharsets.UTF_8));
        Files.write(cafe, word.getBytes(StandardCharsets.UTF_8));
        System.out.println("hello.txt: " + Files.size(hello) + " bytes for 5 characters");
        System.out.println("cafe.txt: " + Files.size(cafe) + " bytes for " + word.length() + " characters");
        Files.delete(hello);
        Files.delete(cafe);
        Files.delete(dir);
    }
}

How It Works

  1. ASCII is one byte each. Hello has five letters and takes five bytes.
  2. An escaped accent. \u00e9 keeps the source file plain ASCII while producing the e-acute character.
  3. Two bytes for e-acute. UTF-8 needs two bytes for that letter, so four characters become five bytes.

Example 2 — Total Size of All Files in a Tree

Walk the tree, keep regular files, and add their sizes, printing each file in sorted order.

Java
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class SizeTotal {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path sub = Files.createDirectory(dir.resolve("b"));
        Path a = dir.resolve("a.txt");
        Path c = sub.resolve("c.txt");
        Path d = sub.resolve("d.txt");
        Files.write(a, "12345".getBytes(StandardCharsets.UTF_8));
        Files.write(c, "1234567890".getBytes(StandardCharsets.UTF_8));
        Files.createFile(d);
        List<Path> files;
        try (Stream<Path> tree = Files.walk(dir)) {
            files = tree.filter(Files::isRegularFile).sorted().collect(Collectors.toList());
        }
        long total = 0;
        for (Path f : files) {
            long n = Files.size(f);
            total += n;
            System.out.println(dir.relativize(f).toString().replace('\\', '/') + ": " + n);
        }
        System.out.println("Total: " + total + " bytes");
        Files.delete(d);
        Files.delete(c);
        Files.delete(a);
        Files.delete(sub);
        Files.delete(dir);
    }
}

How It Works

  1. Files only. Folder sizes differ by system and do not include their contents, so only regular files are measured.
  2. Collect, then measure. Files.size throws a checked exception, so the paths are collected first and measured in an ordinary loop.
  3. Running total. 5 + 10 + 0 gives 15 bytes; the empty d.txt still appears with size 0.

Example 3 — Watch a File Grow, Then Measure Nothing

Append to a file and measure it again; then ask for the size of a file that does not exist.

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

public class SizeGrow {
    public static void main(String[] args) throws IOException {
        Path dir = Files.createTempDirectory("demo");
        Path log = dir.resolve("app.log");
        Files.write(log, "abc".getBytes(StandardCharsets.UTF_8));
        System.out.println("After first write: " + Files.size(log));
        Files.write(log, "defg".getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
        System.out.println("After append: " + Files.size(log));
        try {
            Files.size(dir.resolve("none.log"));
        } catch (NoSuchFileException e) {
            System.out.println("none.log: NoSuchFileException");
        }
        Files.delete(log);
        Files.delete(dir);
    }
}

How It Works

  1. Start small. Three ASCII letters make a 3-byte file.
  2. APPEND adds to the end. Four more bytes bring the size to 7 instead of replacing the content.
  3. Missing is an error. For a file that does not exist, size throws NoSuchFileException instead of returning 0.

Frequently Asked Questions

Call Files.size(path). It returns the size in bytes as a long.
Nothing; it throws NoSuchFileException. File.length() from java.io would return 0 instead.
Files.size counts bytes. In UTF-8, characters outside ASCII take two to four bytes each.
Walk the folder with Files.walk, keep regular files, and add up Files.size for each. Calling size on the folder itself does not include its contents.
Yes. It returns the size of the target file.

Next: Files.walk()

Stream every file and folder in a directory tree, depth-first (Java 8+).

walk() 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.

7 people found this page helpful