Skip to content

Java file size with Files.size()

Ramesh Fadatare edited this page Jul 12, 2019 · 1 revision

Files has the size() method to determine the size of the file. This is the most recent API and it is recommended for new Java applications.

Java file size with Files

The code example determines the file size using Files' size() method.

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class JavaFileSizeEx3 {

    public static void main(String[] args) throws IOException {

        String fileName = "src/main/resources/words.txt";

        Path filePath = Paths.get(fileName);
        long fileSize = Files.size(filePath);        

        System.out.format("The size of the file: %d bytes", fileSize);
    }
}

Output:

The size of the file: 200 bytes
Clone this wiki locally