I have a file on a server and it is a zip file. How to check if the file size is bigger than 27 MB?
File file = new File("U:\intranet_root\intranet\R1112B2.zip");
if (file > 27) {
//do something
}
I have a file on a server and it is a zip file. How to check if the file size is bigger than 27 MB?
File file = new File("U:\intranet_root\intranet\R1112B2.zip");
if (file > 27) {
//do something
}
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.
More info on API : http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
file.length() will return you the length in bytes, then you divide that by 1048576, and now you've got megabytes!
You can use
FileChannel
inJava
.FileChannel has the size() method to determine the size of the file.
Or you can determine the file size using
Apache Commons
' FileUtils' sizeOf() method. If you are using maven, add this topom.xml
file.Try the following coding,
These methods will output the size in Bytes. So to get the MB size, you need to divide the file size from (1024*1024).
Now you can simply use the
if-else
conditions since the size is captured in MB.You can use substring to get portio of String which is equal to 1 mb:
Try following code: