I have a directory on network drive with million of files. If I try to read it with
folder.listFiles()
it will take a lot of time until resulting array will be filled with files.
I would like to receive files by one and printout a progress.
How can I do this in Java?
You might try with DirectoryStream
:
Path dir = Paths.get("C:\\"); // directory to list
try (DirectoryStream<Path> stream = Files.newDirectoryStream(dir)) {
for (Path entry: stream) {
System.out.println(entry);
}
} catch (DirectoryIteratorException ex) {
ex.printStackTrace();
} catch (IOException ex) {
ex.printStackTrace();
}
You can also make the DirectoryStream
filter files for you if you need to: all you need to do is add a parameter to the Files.newDirectoryStream
call:
DirectoryStream<Path> stream = Files.newDirectoryStream(dir, "*.{txt,png,jpg}");
You can use the NIO.2 API:
try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(Paths.get("/path/to/directory"))) {
for (Path path : directoryStream) {
System.out.println(path.toString());
}
} catch (IOException ex) {}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.