java有效地获取文件大小

标签 java filesize

在谷歌搜索时,我看到使用 java.io.File#length()可能很慢。 FileChannel有一个 size()方法也是可用的。

java中有没有一种有效的方法来获取文件大小?

最佳答案

好吧,我试着用下面的代码来衡量它:

对于运行 = 1 和迭代 = 1,URL 方法在大多数情况下最快,其次是 channel 。我在大约 10 次暂停的情况下运行此程序。所以对于一次性访问,使用URL是我能想到的最快的方式:

LENGTH sum: 10626, per Iteration: 10626.0

CHANNEL sum: 5535, per Iteration: 5535.0

URL sum: 660, per Iteration: 660.0

对于运行 = 5 和迭代 = 50,图片绘制不同。

LENGTH sum: 39496, per Iteration: 157.984

CHANNEL sum: 74261, per Iteration: 297.044

URL sum: 95534, per Iteration: 382.136

文件必须缓存对文件系统的调用,而 channel 和 URL 有一些开销。

代码:

import java.io.*;
import java.net.*;
import java.util.*;

public enum FileSizeBench {

    LENGTH {
        @Override
        public long getResult() throws Exception {
            File me = new File(FileSizeBench.class.getResource(
                    "FileSizeBench.class").getFile());
            return me.length();
        }
    },
    CHANNEL {
        @Override
        public long getResult() throws Exception {
            FileInputStream fis = null;
            try {
                File me = new File(FileSizeBench.class.getResource(
                        "FileSizeBench.class").getFile());
                fis = new FileInputStream(me);
                return fis.getChannel().size();
            } finally {
                fis.close();
            }
        }
    },
    URL {
        @Override
        public long getResult() throws Exception {
            InputStream stream = null;
            try {
                URL url = FileSizeBench.class
                        .getResource("FileSizeBench.class");
                stream = url.openStream();
                return stream.available();
            } finally {
                stream.close();
            }
        }
    };

    public abstract long getResult() throws Exception;

    public static void main(String[] args) throws Exception {
        int runs = 5;
        int iterations = 50;

        EnumMap<FileSizeBench, Long> durations = new EnumMap<FileSizeBench, Long>(FileSizeBench.class);

        for (int i = 0; i < runs; i++) {
            for (FileSizeBench test : values()) {
                if (!durations.containsKey(test)) {
                    durations.put(test, 0l);
                }
                long duration = testNow(test, iterations);
                durations.put(test, durations.get(test) + duration);
                // System.out.println(test + " took: " + duration + ", per iteration: " + ((double)duration / (double)iterations));
            }
        }

        for (Map.Entry<FileSizeBench, Long> entry : durations.entrySet()) {
            System.out.println();
            System.out.println(entry.getKey() + " sum: " + entry.getValue() + ", per Iteration: " + ((double)entry.getValue() / (double)(runs * iterations)));
        }

    }

    private static long testNow(FileSizeBench test, int iterations)
            throws Exception {
        long result = -1;
        long before = System.nanoTime();
        for (int i = 0; i < iterations; i++) {
            if (result == -1) {
                result = test.getResult();
                //System.out.println(result);
            } else if ((result = test.getResult()) != result) {
                 throw new Exception("variance detected!");
             }
        }
        return (System.nanoTime() - before) / 1000;
    }

}

关于java有效地获取文件大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/116574/

相关文章:

java - OWLAPI : Want to get inferred axioms from ontology using HermiT reasoner

java - 使用非常高的 CPU 和内存的 Elasticsearch Java

java - 在运行时从 WSDL 动态调用 Web 服务

java - 谷歌应用引擎: Disabling Precompilation

linux - 如何找到带有标题信息的 ELF 文件/图像的大小?

python - 安全与文件大小

java - SaxParser 无法解析 url

file - 很多小文件还是几个大文件?

ruby-on-rails - Ruby on Rails 日志文件太大

powershell - 添加Get-ChildItem列表的文件大小