Sunday, 14 February 2016

Print File size using Java

If you ever want to print the file size using Java, use the following piece of code below :

import java.io.*;

public class FileSize {

public static void main(String[] args) {
File f = new File("//Users//ironcladzone//Downloads//image1.jpg");
System.out.println("File size in bytes = " +f.length());
System.out.println("\nFile size in kb = " +f.length()/1024);

}


}

Output :

File size in bytes = 241833

File size in kb = 236

Note that when we use the length() function, it gives us the file size in bytes. For getting output in kilobytes, divide it by 1024. If you want size in megabytes, again divide it by another 1024. So on and so forth...

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...
eXTReMe Tracker