如何在MB(Megabytes)中获取文件的大小?

问题描述 投票:0回答:12
如何检查文件大小是否大于27 MB?

File file = new File("U:\intranet_root\intranet\R1112B2.zip"); if (file > 27) { //do something }

使用
length()
java file url filesize
12个回答
200
投票
File

您可以将转换结合到一个步骤中,但是我试图完全说明过程。


trory遵循代码:
// 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) {
  ...
}


示例:


60
投票


最初是使用来自Apache Commons-io的fileutil。


51
投票
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()

将在字节中返回您的长度,然后将其除以

16
投票
,现在您已经有了Megabytes!

您可以用

文件#length()

检索文件的长度,该flength()将返回字节中的值,因此您需要将其除以1024*1024以在MB中获取其值。


11
投票
由于Java 7

您可以使用File fileObj = new File(filePathString); String fileSizeReadable = FileUtils.byteCountToDisplaySize(fileObj.length()); // output will be like 56 MB

java.nio.file.Files.size(Path p)

4
投票

您可以做这样的事情: 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"); }


4
投票

public static String getSizeLabel(Integer size) { String cnt_size = "0"; double size_kb = size / 1024; double size_mb = size_kb / 1024; double size_gb = size_mb / 1024; if (Math.floor(size_gb) > 0) { try { String[] snplit = String.valueOf((size_gb)).split("\\."); cnt_size = snplit[0] + "." + snplit[1].substring(0, 2) + "GB"; } catch (Exception e) { cnt_size = String.valueOf(Math.round(size_gb)) + "GB"; } } else if (Math.floor(size_mb) > 0) { try { String[] snplit = String.valueOf((size_mb)).split("\\."); cnt_size = snplit[0] + "." + snplit[1].substring(0, 2) + "MB"; } catch (Exception e) { cnt_size = String.valueOf(Math.round(size_mb)) + "MB"; } } else { cnt_size = String.valueOf(Math.round(size_kb)) + "KB"; } return cnt_size; }

Kotlin扩展解决方案
将这些添加到某个地方,然后致电

Integer filesize = new File("path").length(); getSizeLabel(filesize) // Output 16.02MB
或您需要的任何一个:

4
投票

如果您想更轻松地使用字符串或URI工作,请尝试添加这些:

val File.size get() = if (!exists()) 0.0 else length().toDouble()
val File.sizeInKb get() = size / 1024
val File.sizeInMb get() = sizeInKb / 1024
val File.sizeInGb get() = sizeInMb / 1024
val File.sizeInTb get() = sizeInGb / 1024

如果您想轻松地将值显示为字符串,则这些是简单的包装器。随意自定义显示的默认小数

fun Uri.asFile(): File = File(toString())

fun String?.asUri(): Uri? {
    try {
        return Uri.parse(this)
    } catch (e: Exception) {
    }
    return null
}

2
投票

fun File.sizeStr(): String = size.toString() fun File.sizeStrInKb(decimals: Int = 0): String = "%.${decimals}f".format(sizeInKb) fun File.sizeStrInMb(decimals: Int = 0): String = "%.${decimals}f".format(sizeInMb) fun File.sizeStrInGb(decimals: Int = 0): String = "%.${decimals}f".format(sizeInGb) fun File.sizeStrWithBytes(): String = sizeStr() + "b" fun File.sizeStrWithKb(decimals: Int = 0): String = sizeStrInKb(decimals) + "Kb" fun File.sizeStrWithMb(decimals: Int = 0): String = sizeStrInMb(decimals) + "Mb" fun File.sizeStrWithGb(decimals: Int = 0): String = sizeStrInGb(decimals) + "Gb"

API上的更多信息:

Http://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/fileutils.html


    

您可以使用子字符串获得等于1 Mb的字符串端口:

public static long sizeOf(File file)
    

您可以在

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); } }
中使用
FileChannel

0
投票
Filechannel具有确定文件大小的方法。
Java

或您可以使用'fileutils'sizeof()方法来确定文件大小。如果您使用的是Maven,请将其添加到 String fileName = "D://words.txt"; Path filePath = Paths.get(fileName); FileChannel fileChannel = FileChannel.open(filePath); long fileSize = fileChannel.size(); System.out.format("The size of the file: %d bytes", fileSize); 文件中。


0
投票

尝试以下编码,

pom.xml
这些方法将输出字节中的大小。因此,要获取MB大小,您需要将文件大小与(1024*1024)划分。

0
投票
现在,您可以简单地使用

<dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency>

条件,因为大小是在MB中捕获的。 

String fileName = "D://words.txt"; File f = new File(fileName); long fileSize = FileUtils.sizeOf(f); System.out.format("The size of the file: %d bytes", fileSize);

注:1048576 =(1024*1024)= 1MB 输出:文件大小小于1Mb

如果已经在答案中被告知,但是IMO会是:

if-else
;


最新问题
© www.soinside.com 2019 - 2025. All rights reserved.