Get file size in Java example shows how to get file size in Java. The example also shows how to get file size in bytes, KB, MB, GB and TB in Java.
How to get file size in Java?
We can use the Java File class to get the file size. File class provides the length
method that returns the file’s size in bytes.
1 |
public long length() |
This method returns the size of the file in bytes.
Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.javacodeexamples.ioexamples; import java.io.File; public class GetFileSizeExample { public static void main(String[] args) { String strFilePath = "C:/file_size_demo.txt"; //File object denoting the file File file = new File(strFilePath); /* * To get the file size, use length method of * the File class. This method returns size of file * in bytes. */ long lSizeInBytes = file.length(); System.out.println("File size in bytes: " + lSizeInBytes); } } |
Output
1 |
File size in bytes: 11947 |
Note:
If the file does not exist, the length
method returns 0. To differentiate it from the actual empty file, always check if the file exists using the exists
method of the File class before getting the size.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
//file path String strFilePath = "C:/file_size_demo.txt"; //File object denoting the file File file = new File(strFilePath); if(file.exists()){ long lSizeInBytes = file.length(); System.out.println("File size in bytes: " + lSizeInBytes); }else{ System.out.println("File does not exists"); } |
How to get file size in KB, MB, GB, and TB in Java?
Once we get the file size in bytes, it is fairly easy to convert it to kilobytes (KB), megabytes (MB), gigabytes (GB), and terabytes (TB) as given below.
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
package com.javacodeexamples.ioexamples; import java.io.File; public class GetFileSizeExample { public static void main(String[] args) { //file path String strFilePath = "C:/data.sql.gz"; //File object denoting the file File file = new File(strFilePath); if(file.exists()){ long lSizeInBytes = file.length(); System.out.println("File size in bytes: " + lSizeInBytes); //divide bytes by 1024 to get size in kilobytes double dSizeInKB = lSizeInBytes/1024; System.out.println("File size in kilobytes (KB): " + dSizeInKB); //divide kilobytes by 1024 to get size in megabytes double dSizeInMB = dSizeInKB/1024; System.out.println("File size in megabytes (MB): " + dSizeInMB); //divide megabytes by 1024 to get size in gigabytes double dSizeInGB = dSizeInMB/1024; System.out.println("File size in gigabytes (GB): " + dSizeInGB); //divide gigabytes by 1024 to get size in terabytes double dSizeInTB = dSizeInGB/1024; System.out.println("File size in terabytes (TB): " + dSizeInTB); }else{ System.out.println("File does not exists"); } } } |
Output
1 2 3 4 5 |
File size in bytes: 11914440 File size in kilobytes (KB): 11635.0 File size in megabytes (MB): 11.3623046875 File size in gigabytes (GB): 0.011096000671386719 File size in terabytes (TB): 1.0835938155651093E-5 |
Important Note:
The File length
method throws the SecurityException
if the file is not readable or there is no read access.
This example is a part of the Java File tutorial.
Please let us know your views in the comments section below.