如何在Java中获取文件大小 [英] How to get file size in Java

查看:1471
本文介绍了如何在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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆