File Size
# Java Example - Get File Size
[ Java Example](#)
The following example demonstrates using the `file.exists()` and `file.length()` methods of the `File` class to get the file size in bytes (1KB=1024 bytes):
## Main.java File
import java.io.File; public class Main{public static long getFileSize(String filename){File file = new File(filename); if(!file.exists() || !file.isFile()){System.out.println("File does not exist"); return -1; }return file.length(); }public static void main(String[]args){long size = getFileSize("c:/java.txt"); System.out.println("java.txt file size is: " + size); }}
The output of the above code is (assuming java.txt is located on the C drive):
java.txt file size is: 480
[ Java Example](#)
YouTip