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
}
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.
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);
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 "";
}
file.length() will return you the length in bytes, then you divide that by 1048576, and now you've got megabytes!
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
You can retrieve the length of the file with File#length(), which will return a value in bytes, so you need to divide this by 1024*1024 to get its value in mb.
Since Java 7 you can use java.nio.file.Files.size(Path p)
.
Path path = Paths.get("C:\\1.txt");
long expectedSizeInMB = 27;
long expectedSizeInBytes = 1024 * 1024 * expectedSizeInMB;
long sizeInBytes = -1;
try {
sizeInBytes = Files.size(path);
} catch (IOException e) {
System.err.println("Cannot get the size - " + e);
return;
}
if (sizeInBytes > expectedSizeInBytes) {
System.out.println("Bigger than " + expectedSizeInMB + " MB");
} else {
System.out.println("Not bigger than " + expectedSizeInMB + " MB");
}
public static long sizeOf(File file)
More info on API : http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/FileUtils.html
You can use substring to get portio of String which is equal to 1 mb:
public static void main(String[] args) {
// Get length of String in bytes
String string = "long string";
long sizeInBytes = string.getBytes().length;
int oneMb=1024*1024;
if (sizeInBytes>oneMb) {
String string1Mb=string.substring(0, oneMb);
}
}
来源:https://stackoverflow.com/questions/8989746/how-to-get-size-of-file-in-mb