英文:
IllegalStateException occurs, writing a stream
问题
以下是您代码中的翻译:
private static void writeAllTxtSorted(String fileId) {
try {
Path file = tmp.resolve("AllTxt" + fileId);
Files.deleteIfExists(file);
69-> Files.write(file, find().sorted()::iterator, StandardOpenOption.CREATE);
} catch (Exception e) {
err(e, "problems", l);
}
}
static Stream<CharSequence> find() throws IOException {
try (Stream<Path> walk = Files.walk(Path.of("C:/Temp"))) {
return walk.map(pth -> pth.toString());
}
}
tmp
是 java.io.temp
。
err
是一个将错误记录在 Logger l
上的操作。
这个简化的代码应该将位于 C:\Temp
以下的所有文件路径写入一个文件。
但它抛出了 IllegalStateException
异常。
英文:
What am I doing wrong here:
private static void writeAllTxtSorted(String fileId) {
try {
Path file = tmp.resolve("AllTxt" + fileId);
Files.deleteIfExists(file);
69-> Files.write(file, find().sorted()::iterator, StandardOpenOption.CREATE);
} catch (Exception e) {
err(e, "problems", l);
}
}
static Stream<CharSequence> find() throws IOException {
try (Stream<Path> walk = Files.walk(Path.of("C:/Temp"))) {
return walk.map(pth -> pth.toString());
}
}
java.lang.IllegalStateException null
[java.base/java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:102)
java.base/java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1855)
java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$0(StreamSpliterators.java:292)
java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:298)
java.base/java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
java.base/java.nio.file.Files.write(Files.java:3583)
java.base/java.nio.file.Files.write(Files.java:3633)
eu.ngong.allTxt/eu.ngong.allTxt.App.writeAllTxtSorted(App.java:69)
eu.ngong.allTxt/eu.ngong.allTxt.App.main(App.java:50)]
tmp
is java.io.temp
.<BR/>
err
is an operation logging the error on the Logger l
.
What this reduced code should do is, write all paths of files below C:\Temp
to a file.<BR/>
Instead it throws the exception IllegalStateException
.
答案1
得分: 4
不要在find
方法中关闭Stream<Path>
,因为当前的做法是在try-with-resources中关闭它。
根据文档(重点是我的):
<blockquote>返回的流包含对一个或多个打开目录的引用。这些目录会在关闭流时关闭。
API 注意:
必须在try-with-resources语句或类似的控制结构中使用此方法,以确保流的打开目录在流的操作完成后立即关闭。</blockquote>
static Stream<CharSequence> find() throws IOException {
return Files.walk(Path.of("C:/Temp")).map(Path::toString);
}
相反,你可以在writeAllTxtSorted
中关闭它。
try (var walk = find().sorted()) {
Files.write(file, walk::iterator, StandardOpenOption.CREATE);
}
英文:
Don't close the Stream<Path>
returned by walk inside the find
method (as is currently being done by the try-with-resources).
According to the documentation (emphasis mine):
<blockquote>The returned stream contains references to one or more open directories. The directories are closed by closing the stream.
API Note:
This method must be used within a try-with-resources statement or similar control structure to ensure that the stream's open directories are closed promptly after the stream's operations have completed.</blockquote>
static Stream<CharSequence> find() throws IOException {
return Files.walk(Path.of("C:/Temp")).map(Path::toString);
}
You can close it in writeAllTxtSorted
instead.
try (var walk = find().sorted()) {
Files.write(file, walk::iterator, StandardOpenOption.CREATE);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论