java.io.File.length() Java Code Examples
How to use length() method in java.io.File class.
More Java code examples for java.io.File
Java Code Example 1 - Get File Size
import java.io.File;
public class GetFileSizeExample2 {
public static void main(String[] args) {
String fileLocation = "D:\\SimpleSolution\\logo.png";
File file = new File(fileLocation);
if(file.exists()) {
long fileSizeInBytes = file.length();
double fileSizeInKiloBytes = fileSizeInBytes / 1024.0;
double fileSizeInMegaBytes = fileSizeInKiloBytes / 1024.0;
double fileSizeInGigaBytes = fileSizeInMegaBytes / 1024.0;
System.out.println("File size: " );
System.out.println(fileSizeInBytes + " bytes");
System.out.println(fileSizeInKiloBytes + " KB");
System.out.println(fileSizeInMegaBytes + " MB");
System.out.println(fileSizeInGigaBytes + " GB");
} else {
System.out.println("File does not exists.");
}
}
}
Output:
File size:
31281 bytes
30.5478515625 KB
0.029831886291503906 MB
2.9132701456546783E-5 GB