Files.walkFileTree中的遍历顺序 [英] Order of traversal in Files.walkFileTree

查看:1201
本文介绍了Files.walkFileTree中的遍历顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Files.walkFileTree 在同一级别访问文件/目录的顺序是什么?

What is the order in which Files.walkFileTree visits files/directories at the same level?

它似乎没有按大小,上次修改时间或名称的顺序访问它们。我在 API文档

It doesn't appear to visit them in order of size, last modified time or name. I couldn't find anything in the API documentation either.

也许 preVisitDirectory 方法可用于指定访问顺序但默认行为是什么?

Perhaps the preVisitDirectory method can be used to specify the order of visit but what is default behaviour ?

推荐答案

订单在 Java教程中,未根据此注释定义读取子目录的内容:

The order in which the subdirectories are read is not defined as per this comment in the Java Tutorial:


首先深入了解文件树,但不能对访问子目录的迭代顺序做任何假设。

A file tree is walked depth first, but you cannot make any assumptions about the iteration order that subdirectories are visited.

至于读取文件的顺序,它取决于(在当前实现中)提供的 DirectoryStream ,这是m上的$ code> sun.nio.fs.WindowsDirectoryStream 电脑。阅读 DirectoryStream的javadoc ,您将看到:

As for the order in which the files are read, it depends (in the current implementation) on the supplied DirectoryStream, which is sun.nio.fs.WindowsDirectoryStream on my computer. Reading the javadoc of DirectoryStream, you will see that:


迭代器返回的元素没有特定的顺序。

The elements returned by the iterator are in no specific order.

这篇关于Files.walkFileTree中的遍历顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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