Java File Size with Files.size()

🎓 Check Out My Top 25 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare

Files class have the Files.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 the Files' size() method.
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

public class JavaFileSizeExample {

    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

My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:

Comments

Spring Boot 3 Paid Course Published for Free
on my Java Guides YouTube Channel

Subscribe to my YouTube Channel (165K+ subscribers):
Java Guides Channel

Top 10 My Udemy Courses with Huge Discount:
Udemy Courses - Ramesh Fadatare