天道酬勤,学无止境

directorystream

List directory content with Project Reactor and DirectoryStream

I'd like to use DirectoryStream with Project Reactor to list all the files in a directory. My try is: Path myDir = Paths.get("C:\\Users\\r.dacanal\\Documents\\Reply\\EDA\\logging-consumer\\input"); DirectoryStream<Path> directoryStream = Files.newDirectoryStream(myDir); Flux.fromIterable(directoryStream).doOnNext(s -> System.out.println(s)).subscribe(); But I'm getting the following Exception: Caused by: java.lang.IllegalStateException: Iterator already obtained at sun.nio.fs.WindowsDirectoryStream.iterator(WindowsDirectoryStream.java:117) ~[na:1.8.0_171] at reactor.core.publisher.FluxIterable

2021-05-13 19:38:58    分类:问答    project-reactor   flux   directorystream

使用nio.file.DirectoryStream递归列出目录中的所有文件;(Recursively list all files within a directory using nio.file.DirectoryStream;)

问题 我想列出指定目录中的所有文件以及该目录中的子目录。 没有目录应列出。 我当前的代码如下。 它仅列出指定目录中的文件和目录,因此无法正常工作。 我怎样才能解决这个问题? final List<Path> files = new ArrayList<>(); Path path = Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles"); try { DirectoryStream<Path> stream; stream = Files.newDirectoryStream(path); for (Path entry : stream) { files.add(entry); } stream.close(); } catch (IOException e) { e.printStackTrace(); } for (Path entry: files) { System.out.println(entry.toString()); } 回答1 Java 8为此提供了一种不错的方法: Files.walk(path) 此方法返回Stream<Path> 。 回答2 制作一个方法,如果下一个元素是目录,该方法将自行调用 void listFiles(Path path) throws

2021-04-07 15:23:52    分类:技术分享    java   file   nio   directorystream

Recursively list all files within a directory using nio.file.DirectoryStream;

I want to list all the FILES within the specified directory and subdirectories within that directory. No directories should be listed. My current code is below. It does not work properly as it only lists the files and directories within the specified directory. How can I fix this? final List<Path> files = new ArrayList<>(); Path path = Paths.get("C:\\Users\\Danny\\Documents\\workspace\\Test\\bin\\SomeFiles"); try { DirectoryStream<Path> stream; stream = Files.newDirectoryStream(path); for (Path entry : stream) { files.add(entry); } stream.close(); } catch (IOException e) { e.printStackTrace()

2021-03-25 02:13:06    分类:问答    java   file   nio   directorystream