How to get the size of a file in MB (Megabytes)?

165,299

Solution 1

Use the length() method of the File class to return the size of the file in bytes.

// Get file from file name
File file = new File("U:\intranet_root\intranet\R1112B2.zip");

// Get length of file in bytes
long fileSizeInBytes = file.length();
// Convert the bytes to Kilobytes (1 KB = 1024 Bytes)
long fileSizeInKB = fileSizeInBytes / 1024;
// Convert the KB to MegaBytes (1 MB = 1024 KBytes)
long fileSizeInMB = fileSizeInKB / 1024;

if (fileSizeInMB > 27) {
  ...
}

You could combine the conversion into one step, but I've tried to fully illustrate the process.

Solution 2

Try following code:

File file = new File("infilename");

// Get the number of bytes in the file
long sizeInBytes = file.length();
//transform in MB
long sizeInMb = sizeInBytes / (1024 * 1024);

Solution 3

Example :

public static String getStringSizeLengthFile(long size) {

    DecimalFormat df = new DecimalFormat("0.00");

    float sizeKb = 1024.0f;
    float sizeMb = sizeKb * sizeKb;
    float sizeGb = sizeMb * sizeKb;
    float sizeTerra = sizeGb * sizeKb;


    if(size < sizeMb)
        return df.format(size / sizeKb)+ " Kb";
    else if(size < sizeGb)
        return df.format(size / sizeMb) + " Mb";
    else if(size < sizeTerra)
        return df.format(size / sizeGb) + " Gb";

    return "";
}

Solution 4

Easiest is by using FileUtils from Apache commons-io.( https://commons.apache.org/proper/commons-io/javadocs/api-2.5/org/apache/commons/io/FileUtils.html )

Returns human readable file size from Bytes to Exabytes , rounding down to the boundary.

File fileObj = new File(filePathString);
String fileSizeReadable = FileUtils.byteCountToDisplaySize(fileObj.length());

// output will be like 56 MB 

Solution 5

file.length() will return you the length in bytes, then you divide that by 1048576, and now you've got megabytes!

Share:
165,299

Related videos on Youtube

itro
Author by

itro

I do software .

Updated on February 10, 2022

Comments

  • itro
    itro over 2 years

    I have a zip file on a server.

    How can I check if the file size is larger than 27 MB?

    File file = new File("U:\intranet_root\intranet\R1112B2.zip");
    if (file > 27) {
       //do something
    }
    
  • Scadge
    Scadge about 9 years
    This method returns File (or directory) size in bytes, not in megabytes: commons.apache.org/proper/commons-io/apidocs/org/apache/comm‌​ons/…. In order to get size in megabytes, you still need to divide by 1024 twice.
  • TapanHP
    TapanHP over 6 years
    Thanks for this multiplication, This saves me from writing (1024*1024), saved 4 keystrokes!! :D
  • Buffalo
    Buffalo about 5 years
    Lots of unneeded stuff being done, but I guess it's easier for a newbie to understand.
  • yongchang
    yongchang about 3 years
    jsfiddle.net/darkkyoun/3g71k6ho/16 I did a fiddle base on your code, so whoever need it, can use the fiddle to test it out
  • KenobiBastila
    KenobiBastila about 2 years
    file.length() returns a long value, not Integer.