Get File size in bytes in Java
Description
The following code shows how to get File size in bytes.
Example
/*from w ww. j a v a2 s.c om*/
import java.io.File;
public class Main {
public static void main(String[] args) {
File file = new File("C:/demo.txt");
long fileSize = file.length();
System.out.println(fileSize + " bytes");
System.out.println((double) fileSize / 1024 + " KB");
System.out.println((double) fileSize / (1024 * 1024) + "MB");
}
}
The code above generates the following result.