In this tutorial, you will learn how to get the size of a file in Java, using size() method of java.nio.file.Files class.

Java – Get file size

To get the size of a file given by a path in Java, prepare Path object for the given file, call size() method of the java.nio.file.Files class and pass the file path object as argument to the method. The size() method returns a number (of long type) representing the size of the given file.

If filePath is the given file path, then use the following expression to get the size of file.

</>
Copy
Files.size(filePath)

1. Get the size of file “data.txt” in Java

In the following program, we take a file path "/Users/tutorialkart/data.txt", get the size of this file, and print the size to standard output.