Home Java Files Methods size() Java Files size() Method 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:
long bytes = Files.size(dir.resolve("photo.jpg"));
double kb = bytes / 1024.0; Call What 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 public static long size(Path path) throws IOException Parameters Parameter Description pathThe file to measure. Symbolic links are followed.
Return Value Return Value Description longThe file size in bytes.
Important
Throws NoSuchFileException if the file does not exist and other IOExceptions if it cannot be read. The size of a folder is implementation specific (often 4096 on Linux, 0 on Windows). Sizes of files written with Files.write(path, lines) differ between Windows and Unix because of the line separator.
Compare with Similar Methods Measuring data:
Java Examples 1. Bytes vs characters 2. Add up a folder 3. Growing and missing files 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.
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);
}
} Run with java SizeBytes. The output is the same on Windows, macOS, and Linux.
hello.txt: 5 bytes for 5 characters
cafe.txt: 5 bytes for 4 characters How It Works ASCII is one byte each. Hello has five letters and takes five bytes.An escaped accent. \u00e9 keeps the source file plain ASCII while producing the e-acute character.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.
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);
}
} Run with java SizeTotal. The output is the same on Windows, macOS, and Linux.
a.txt: 5
b/c.txt: 10
b/d.txt: 0
Total: 15 bytes How It Works Files only. Folder sizes differ by system and do not include their contents, so only regular files are measured.Collect, then measure. Files.size throws a checked exception, so the paths are collected first and measured in an ordinary loop.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.
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);
}
} Run with java SizeGrow. The output is the same on Windows, macOS, and Linux.
After first write: 3
After append: 7
none.log: NoSuchFileException How It Works Start small. Three ASCII letters make a 3-byte file.APPEND adds to the end. Four more bytes bring the size to 7 instead of replacing the content.Missing is an error. For a file that does not exist, size throws NoSuchFileException instead of returning 0.Frequently Asked Questions How do I get the size of a file in Java? Call Files.size(path). It returns the size in bytes as a long.
What does Files.size return for a missing file? Nothing; it throws NoSuchFileException. File.length() from java.io would return 0 instead.
Why is the file size larger than the number of characters? Files.size counts bytes. In UTF-8, characters outside ASCII take two to four bytes each.
How do I get the size of a folder? 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.
Does Files.size follow symbolic links? 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 Developer, cloud engineer, and technical writer
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
Helpful Share Copy link Suggestion