Java Program to Get the File Size
import java.io.File;
public class Main
{
public static void main(String[] args)
{
//declare a String variable filePath and assign the path of file
//for which file you want to know the size
String filePath = "F:\\Tutorial IoT\\SDN Cisco PPT.pdf";
//Create a object of File class and pass that filePath as parameter
File file = new File(filePath);
//Check if file does not exist by using inbuilt exists() method
//or check if file is not a normal file type by using inbuilt isFile() method
//then return
if (!file.exists() || !file.isFile())
return;
//else get the file size by using length() method of File class
System.out.println("Your File size is: ");
System.out.println("In Bytes: "+file.length() + " bytes");
System.out.println("In KiloBytes: "+ file.length() / 1024 + " kb");
System.out.println("In MegaBytes: "+file.length() / (1024 * 1024) + " mb");
}
}
Output:
Your File size is:
In Bytes: 11261028 bytes
In KiloBytes: 10997 kb
In MegaBytes: 10 mb
