Finding Size of a File (in bytes) | Java Program

Finding Size of a File (in bytes) | Java Program

How to find out the size of a file using Java Programming ?
Its very easy to do so just use this code :

import java.io.File;

public class Main1 {
        public static void main(String[] args) {
            File file = new File("src\\my.txt");  //file location here
            System.out.print(file.length());
        }
}

In the above example I am finding size of a text file named my.txt and when I run this code i got 552 as output which is its file size in bytes. You can see its accuracy by this snapshot:
showing file size in properties

How to Use this Code ?
Just copy the above code and replace src\\my.txt by your file location (with file extension) and all done.

Note : If you are finding size of a file which is in MB's or GB's than don't get scared by the      output because that will be in Bytes :)

1 Comment. Leave new

Make sure you tick the "Notify Me" box below the comment form to be notified of follow up comments and replies.