File Size in Java – Java中的文件大小

最后修改: 2016年 12月 11日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

In this quick tutorial, we’ll learn how to get the size of a file in Java – using Java 7, the new Java 8 and Apache Common IO.

在这个快速教程中,我们将学习如何在Java中获取文件的大小 – 使用Java 7、新的Java 8和Apache Common IO。

Finally – we will also get a human readable representation of the file size.

最后–我们还将得到一个人类可读的文件大小的表示。

2. Standard Java IO

2.标准的Java IO

Let’s start with a simple example of calculating the size of a file – using the File.length() method:

让我们从一个计算文件大小的简单例子开始–使用 File.length()方法。

private long getFileSize(File file) {
    long length = file.length();
    return length;
}

We can test our implementation relatively simply:

我们可以相对简单地测试我们的实现。

@Test
public void whenGetFileSize_thenCorrect() {
    long expectedSize = 12607;
 
    File imageFile = new File("src/test/resources/image.jpg");
    long size = getFileSize(imageFile);
 
    assertEquals(expectedSize, size);
}

Note that, by default, the file sizes is calculated in bytes.

注意,在默认情况下,文件大小是以字节为单位计算的。

3. With Java NIO

3.使用Java NIO

Next – let’s see how to use the NIO library to get the size of the file.

接下来–让我们看看如何使用NIO库来获得文件的大小。

In the following example, we’ll use the FileChannel.size() API to get the size of a file in bytes:

在下面的例子中,我们将使用FileChannel.size() API来获取一个文件的字节数大小。

@Test
public void whenGetFileSizeUsingNioApi_thenCorrect() throws IOException {
    long expectedSize = 12607;
 
    Path imageFilePath = Paths.get("src/test/resources/image.jpg");
    FileChannel imageFileChannel = FileChannel.open(imageFilePath);

    long imageFileSize = imageFileChannel.size();
    assertEquals(expectedSize, imageFileSize);
}

4. With Apache Commons IO

4.使用Apache Commons IO

Next – let’s see how to get the file size using Apache Commons IO. In the following example – we simply use FileUtils.sizeOf() to get the file size:

接下来–让我们看看如何使用Apache Commons IO获得文件大小。在下面的例子中–我们简单地使用FileUtils.sizeOf()来获取文件的大小。

@Test
public void whenGetFileSizeUsingApacheCommonsIO_thenCorrect() {
    long expectedSize = 12607;
 
    File imageFile = new File("src/test/resources/image.jpg");
    long size = FileUtils.sizeOf(imageFile);
 
    assertEquals(expectedSize, size);
}

Note that, for security restricted files, FileUtils.sizeOf() will report the size as zero.

注意,对于安全受限的文件,FileUtils.sizeOf()将报告其大小为零。

5. Human Readable Size

5.人类可读的尺寸

Finally – let’s see how to get a more user readable representation of the file size using Apache Commons IO – not just a size in bytes:

最后–让我们看看如何使用Apache Commons IO获得一个更便于用户阅读的文件大小表示–而不仅仅是以字节为单位的大小。

@Test
public void whenGetReadableFileSize_thenCorrect() {
    File imageFile = new File("src/test/resources/image.jpg");
    long size = getFileSize(imageFile);
 
    assertEquals("12 KB", FileUtils.byteCountToDisplaySize(size));
}

6. Conclusion

6.结论

In this tutorial, we illustrated examples of using Java and Apache Commons IO to calculate the size of a file in the file system.

在本教程中,我们举例说明了使用Java和Apache Commons IO来计算文件系统中的文件大小。

The implementation of these examples can be found in the GitHub project – this is a Maven-based project, so it should be easy to import and run as it is.

这些例子的实现可以在GitHub项目中找到–这是一个基于Maven的项目,所以应该很容易导入并按原样运行。