0

我想将 DirectoryStream 与 Project Reactor 一起使用来列出目录中的所有文件。

我的尝试是:

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();

但我得到以下异常:

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.subscribe(FluxIterable.java:79) ~[reactor-core-3.3.4.RELEASE.jar:3.3.4.RELEASE]
        at reactor.core.publisher.Flux.subscribe(Flux.java:8264) ~[reactor-core-3.3.4.RELEASE.jar:3.3.4.RELEASE]
        at reactor.core.publisher.Flux.subscribeWith(Flux.java:8428) ~[reactor-core-3.3.4.RELEASE.jar:3.3.4.RELEASE]
        at reactor.core.publisher.Flux.subscribe(Flux.java:8235) ~[reactor-core-3.3.4.RELEASE.jar:3.3.4.RELEASE]
        at reactor.core.publisher.Flux.subscribe(Flux.java:8162) ~[reactor-core-3.3.4.RELEASE.jar:3.3.4.RELEASE]
        at reactor.core.publisher.Flux.subscribe(Flux.java:8080) ~[reactor-core-3.3.4.RELEASE.jar:3.3.4.RELEASE]
        at com.example.loggingconsumer.LoggingConsumerApplication.filterDirectory(LoggingConsumerApplication.java:46) ~[classes/:na]
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_171]
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_171]
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_171]
        at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_171]
        at org.springframework.scheduling.support.ScheduledMethodRunnable.run(ScheduledMethodRunnable.java:84) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) ~[spring-context-5.2.5.RELEASE.jar:5.2.5.RELEASE]
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [na:1.8.0_171]
        at java.util.concurrent.FutureTask.runAndReset(FutureTask.java:308) [na:1.8.0_171]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$301(ScheduledThreadPoolExecutor.java:180) [na:1.8.0_171]
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:294) [na:1.8.0_171]
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [na:1.8.0_171]
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [na:1.8.0_171]
        at java.lang.Thread.run(Thread.java:748) [na:1.8.0_171]

有人可以给出如何将 DirectoryStream 与 Project Reactor 一起使用的示例代码吗?

4

2 回答 2

2

订阅重复调用 iterator(),但 DirectoryStream 有一个检查以确保迭代器在分配它自己之前必须为空

@Override
public Iterator<Path> iterator() {
    if (!isOpen) {
        throw new IllegalStateException("Directory stream is closed");
    }
    synchronized (this) {
        if (iterator != null)
            throw new IllegalStateException("Iterator already obtained");
        iterator = new WindowsDirectoryIterator(firstName);
        return iterator;
    }
}

尽管如果您使用的是 Java 8+,那么使用它的理由是 0,newDirectoryStream(dir)因为您可以使用它list(dir)来提供实际的 Stream

以下应该工作

Path myDir = Paths.get("C:\\Users\\r.dacanal\\Documents\\Reply\\EDA\\logging-consumer\\input");
Stream<Path> directoryStream = Files.list(myDir);
Flux.fromStream(directoryStream).doOnNext(System.out::println).subscribe();
于 2020-04-18T10:32:52.273 回答
0

具有大量文件的目录上的 list(dir) 使用太多内存,newDirectoryStream 是有原因的

于 2020-11-26T11:13:02.130 回答