简体   繁体   English

Java 7-此操作的File.listFiles()成本

[英]Java 7 - File.listFiles() cost of this operation

I have a scenario where I have to loop through file contents for multiple purposes. 我有一种情况,我必须出于多种目的遍历文件内容。 My question here is, is it Ok to call file.listFiles() multiple times? 我的问题是,可以多次调用file.listFiles()吗? Is this an expensive operation? 这是一项昂贵的手术吗? Or should I store this this into a variable and use it in all the places? 还是应该将其存储到变量中并在所有地方使用?

The problem here is. 这里的问题是。 I may have 3 or 4 levels. 我可能有3个或4个级别。 In each level, I have to validate or carry some logics. 在每个级别中,我必须验证或执行一些逻辑。 I have to start from the folder 1 and do logic 1 till folder 4. Then again I have to start from folder 1 to perform logic 2 and then for logic 3 and so on. 我必须从文件夹1开始并执行逻辑1直到文件夹4。然后再次必须从文件夹1开始执行逻辑2,然后执行逻辑3,依此类推。

Folder 1
 - Folder 2
 - File1
      - Folder 3
      - File2
           - Folder 4
           - File3

As per my Java experience I dont think operation is costly, It take n time to get all the file, where n is number of file.You can write recursive program to read the all directory of the folder.Recursive code will be easy to write but may little bit slower in performance. 根据我的Java经验,我不认为操作会很昂贵,获取所有文件都需要n时间,其中n是文件数。您可以编写递归程序来读取文件夹的所有目录。递归代码将很容易编写但可能会降低性能。

 There is no guarantee that the name strings in the resulting array will  
 appear in any specific order; they are not, in particular, guaranteed to   
  appear in alphabetical order.  

  Note that the Files class defines the newDirectoryStream method to open a 
  directory and iterate over the names of the files in the directory. This 
  may use less resources when working with very large directories.

See here 看这里

See here Also 另请参阅这里

Bottom Line Up Front (BLUF): Calling file.listFiles() multiple times will likely cause little/no impact on performance. 最底线(BLUF):多次调用file.listFiles()可能对性能几乎没有影响。

I wrote a test case to measure the amount of time it takes to list the contents of a local directory 10,000 times. 我编写了一个测试用例,以衡量列出本地目录内容10,000次所花费的时间。 I used a 2.9Ghz dual-core Windows 10 machine. 我使用了2.9Ghz双核Windows 10计算机。 There were 45 files of varying size in the directory. 目录中有45个大小不同的文件。 Here's the test case: 这是测试用例:

class Foo {

    public static void main(String[] args) {
        File f = Paths.get(System.getProperties().get("user.home")+"").toFile();
        long startTime = System.currentTimeMillis();
        for(int i = 0; i < 10000; i++){
            f.listFiles()[0].toString();
        }
        long endTime = System.currentTimeMillis();
        System.out.println("Total Time = " + (endTime - startTime));
    }
}

It produced the following output on the first run: 在第一次运行时,它产生了以下输出:

Total Time = 1337

This means that it takes less than 1ms to list the files. 这意味着列出文件所需的时间少于1ms。

Use, 采用,

    Path searchPath = Paths.get("c://log");
    try (DirectoryStream<Path> fileList = Files.newDirectoryStream(searchPath)) {
      for (Path path : fileList) {
        System.out.println(path.getFileName());
      }

Its less resource intensive and provides overloaded methods for pattern matching too. 它的资源消耗较少,并且也提供了用于模式匹配的重载方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM