DirectoryStream return 路径中的顺序是怎样的?文件名、最后修改时间、文件大小?
DirectoryStream return path in what kind of order? filename, last modified, filesize?
我正在尝试使用 DirectoryStream 读取文件夹中的多个文件。一共有 10 个条目。
doc_01.txt, doc_02.txt, doc_03.txt, doc_04.txt, doc_05.txt, doc_06.txt, doc_07.txt, doc_08.txt, doc_09.txt, doc_10.txt
我希望按照文件名的顺序读取文件。
DirectoryStream 是否按文件名顺序读取文件?因为这是我得到的结果:
./mydata/doc_01.txt, ./mydata/doc_02.txt, ./mydata/doc_03.txt, ./mydata/doc_04.txt, ./mydata/doc_08.txt, ./mydata/doc_07.txt, ./mydata/doc_09.txt, ./mydata/doc_10.txt, ./mydata/doc_05.txt, ./mydata/doc_06.txt
这是我的代码:
public static void readData(){
Instant start = Instant.now();
System.out.println("Start reading");
Path path = Paths.get(String.join(File.separator, ".", "mydata"));
try(DirectoryStream<Path> stream =
Files.newDirectoryStream(path, "*.txt")
){
for(Path entry : stream){
System.out.println("reading: " +entry.toString());
}
}catch(IOException e){
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
Instant end = Instant.now();
System.out.println("Done in " + Duration.between(start, end).toString());
}
class DirectoryStream 的 javadoc
明确地说:
The elements returned by the iterator are in no specific order.
Some file systems maintain special links to the directory itself
and the directory's parent directory. Entries representing these links
are not returned by the iterator.
因此,如果您需要特定顺序,则由您对 Path
进行排序。
例如,对于按字母顺序排序,您可以这样做:
DirectoryStream<Path> directoryStream = ...;
StreamSupport.stream(directoryStream.spliterator(), false)
.sorted(Comparator.comparing(Path::toString))
.forEach(p -> { System.out.println("reading: " + p); });
我正在尝试使用 DirectoryStream 读取文件夹中的多个文件。一共有 10 个条目。
doc_01.txt, doc_02.txt, doc_03.txt, doc_04.txt, doc_05.txt, doc_06.txt, doc_07.txt, doc_08.txt, doc_09.txt, doc_10.txt
我希望按照文件名的顺序读取文件。 DirectoryStream 是否按文件名顺序读取文件?因为这是我得到的结果:
./mydata/doc_01.txt, ./mydata/doc_02.txt, ./mydata/doc_03.txt, ./mydata/doc_04.txt, ./mydata/doc_08.txt, ./mydata/doc_07.txt, ./mydata/doc_09.txt, ./mydata/doc_10.txt, ./mydata/doc_05.txt, ./mydata/doc_06.txt
这是我的代码:
public static void readData(){
Instant start = Instant.now();
System.out.println("Start reading");
Path path = Paths.get(String.join(File.separator, ".", "mydata"));
try(DirectoryStream<Path> stream =
Files.newDirectoryStream(path, "*.txt")
){
for(Path entry : stream){
System.out.println("reading: " +entry.toString());
}
}catch(IOException e){
System.err.println("Error: " + e.getMessage());
e.printStackTrace();
}
Instant end = Instant.now();
System.out.println("Done in " + Duration.between(start, end).toString());
}
class DirectoryStream 的 javadoc 明确地说:
The elements returned by the iterator are in no specific order. Some file systems maintain special links to the directory itself and the directory's parent directory. Entries representing these links are not returned by the iterator.
因此,如果您需要特定顺序,则由您对 Path
进行排序。
例如,对于按字母顺序排序,您可以这样做:
DirectoryStream<Path> directoryStream = ...;
StreamSupport.stream(directoryStream.spliterator(), false)
.sorted(Comparator.comparing(Path::toString))
.forEach(p -> { System.out.println("reading: " + p); });