Ich habe eine Datei auf einem Server und es ist eine ZIP-Datei ... Wie prüfe ich, ob die Dateigröße größer als 27 MB ist?
File file = new File("U:\intranet_root\intranet\R1112B2.Zip");
if (file > 27) {
//do something
}
Verwenden Sie die length()
-Methode der File
-Klasse, um die Größe der Datei in Byte zurückzugeben.
// 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) {
...
}
Sie können die Konvertierung in einem Schritt kombinieren, aber ich habe versucht, den Prozess vollständig zu veranschaulichen.
Versuchen Sie folgenden 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);
Beispiel:
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 () gibt die Länge in Bytes zurück, dann teilen Sie diese durch 1048576 und jetzt haben Sie Megabytes!
Am einfachsten ist die Verwendung von FileUtils von Apache commons-io. ( Https://commons.Apache.org/proper/commons-io/javadocs/api-2.5/org/Apache/commons/io/FileUtils.html )
Gibt die von Menschen lesbare Dateigröße von Bytes bis Exabytes zurück.
File fileObj = new File(filePathString);
String fileSizeReadable = FileUtils.byteCountToDisplaySize(fileObj.length());
// output will be like 56 MB
Sie können die Länge der Datei mit File # length () abrufen. Dies gibt einen Wert in Byte zurück. Sie müssen diesen Wert durch 1024 * 1024 teilen, um den Wert in mb zu erhalten.
Seit Java 7 können Sie Java.nio.file.Files.size(Path p)
verwenden.
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");
}
Sie können substring verwenden, um einen Teil von String zu erhalten, der 1 mb entspricht:
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);
}
}
public static long sizeOf(File file)
Weitere Informationen zu API: http://commons.Apache.org/proper/commons-io/apidocs/org/Apache/commons/io/FileUtils.html