In this post, we will see how to get size of file in java. We can directly use java.io.File ‘s length method to get its size in bytes. We can convert it to KB and MB accordingly.
When you run above program, you will get following output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
package org.arpit.java2blog; import java.io.File; public class GetSizeOfFileMain { public static void main(String[] args) { // Read the file File mp3File=new File("/Users/Arpit/Desktop/MySong.mp3"); // Use length method to get size of file in bytes double fileSizeinBytes=mp3File.length(); System.out.println( "Size of file in bytes :" +fileSizeinBytes + " bytes"); System.out.println( "Size of file in KB :" +(double)fileSizeinBytes/1024 + " KB"); System.out.println( "Size of file in MB :" +(double)fileSizeinBytes/(1024*1024) + " MB"); } } |
1 2 3 4 5 |
Size of file in bytes :3699969.0 bytes Size of file in KB :3613.2509765625 KB Size of file in MB :3.5285654067993164 MB |
Was this post helpful?
Let us know if this post was helpful. Feedbacks are monitored on daily basis. Please do provide feedback as that\'s the only way to improve.