如何在Java中获取文件大小 [英] How to get file size in Java
问题描述
可能重复:
文件夹或文件的大小
我使用此代码来实例化文件
对象:
I used this code to instantiate a File
object:
File f = new File(path);
如何获得此档案的大小?
How do I get the size of this file?
getUsableSpace()
, getTotalSpace()
和之间有什么区别? getFreeSpace()
?
推荐答案
使用 length()
方法在 文件
课程。来自javadocs:
Use the length()
method in the File
class. From the javadocs:
返回此抽象路径名表示的文件的长度。如果此路径名表示目录,则返回值未指定。
Returns the length of the file denoted by this abstract pathname. The return value is unspecified if this pathname denotes a directory.
对于问题的第二部分,直接来自文件
的javadocs :
For the second part of the question, straight from File
's javadocs:
-
getUsableSpace()
返回可用的字节数此抽象路径名命名的分区上的虚拟机
getUsableSpace()
Returns the number of bytes available to this virtual machine on the partition named by this abstract pathname
getTotalSpace()
返回分区的大小以此抽象路径名命名
getTotalSpace()
Returns the size of the partition named by this abstract pathname
getFreeSpace()
返回指定的分区中未分配的字节数这个抽象的路径名称
getFreeSpace()
Returns the number of unallocated bytes in the partition named by this abstract path name
这篇关于如何在Java中获取文件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!