如何获取以MB为单位的文件大小(兆字节)? [英] How to get the size of a file in MB (Megabytes)?
本文介绍了如何获取以MB为单位的文件大小(兆字节)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在服务器上有一个zip文件.
I have a zip file on a server.
如何检查文件大小是否大于27 MB?
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
}
推荐答案
使用 File
类的 length()
方法返回文件大小(以字节为单位)
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.
这篇关于如何获取以MB为单位的文件大小(兆字节)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文