Java file size with FileUtils.sizeOf()

In this example, we determine the file size with Apache Commons' FileUtils. Its method for finding out the file size is sizeOf().
For this example, we need the commons-io dependency:

Java file size with FileUtils.sizeOf()

The code example determines the file size using Apache Commons' FileUtils' sizeOf() method.

public class JavaFileSizeExample {

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

        String fileName = "src/main/resources/words.txt";
        File f = new File(fileName);

        long fileSize = FileUtils.sizeOf(f);        

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

Free Spring Boot Tutorial - 5 Hours Full Course

Watch this course on YouTube at Spring Boot Tutorial | Fee 5 Hours Full Course