java examples
Home Java Examples Resources Java Interview Questions
Brainpower SEO

How to get the size of a file?

import java.io.File;

public class FileSize
{
    public static void main(String[] args) throws Exception
    {
        File file = new File("Housing.pdf");

        // Get the size of a file in bytes.
        long fileSize = file.length();

        // Using new features in Java to print the filesize
        System.out.printf("File size: %d%n", fileSize);
    }
}