IllegalStateException 发生,写入流

huangapple go评论109阅读模式
英文:

IllegalStateException occurs, writing a stream

问题

以下是您代码中的翻译:

  1. private static void writeAllTxtSorted(String fileId) {
  2. try {
  3. Path file = tmp.resolve("AllTxt" + fileId);
  4. Files.deleteIfExists(file);
  5. 69-> Files.write(file, find().sorted()::iterator, StandardOpenOption.CREATE);
  6. } catch (Exception e) {
  7. err(e, "problems", l);
  8. }
  9. }
  10. static Stream<CharSequence> find() throws IOException {
  11. try (Stream<Path> walk = Files.walk(Path.of("C:/Temp"))) {
  12. return walk.map(pth -> pth.toString());
  13. }
  14. }

tmpjava.io.temp

err 是一个将错误记录在 Logger l 上的操作。

这个简化的代码应该将位于 C:\Temp 以下的所有文件路径写入一个文件。

但它抛出了 IllegalStateException 异常。

英文:

What am I doing wrong here:

  1. private static void writeAllTxtSorted(String fileId) {
  2. try {
  3. Path file = tmp.resolve(&quot;AllTxt&quot; + fileId);
  4. Files.deleteIfExists(file);
  5. 69-&gt; Files.write(file, find().sorted()::iterator, StandardOpenOption.CREATE);
  6. } catch (Exception e) {
  7. err(e, &quot;problems&quot;, l);
  8. }
  9. }
  10. static Stream&lt;CharSequence&gt; find() throws IOException {
  11. try (Stream&lt;Path&gt; walk = Files.walk(Path.of(&quot;C:/Temp&quot;))) {
  12. return walk.map(pth -&gt; pth.toString());
  13. }
  14. }
  1. java.lang.IllegalStateException null
  2. [java.base/java.nio.file.FileTreeIterator.hasNext(FileTreeIterator.java:102)
  3. java.base/java.util.Spliterators$IteratorSpliterator.tryAdvance(Spliterators.java:1855)
  4. java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.lambda$initPartialTraversalState$0(StreamSpliterators.java:292)
  5. java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.fillBuffer(StreamSpliterators.java:206)
  6. java.base/java.util.stream.StreamSpliterators$AbstractWrappingSpliterator.doAdvance(StreamSpliterators.java:161)
  7. java.base/java.util.stream.StreamSpliterators$WrappingSpliterator.tryAdvance(StreamSpliterators.java:298)
  8. java.base/java.util.Spliterators$1Adapter.hasNext(Spliterators.java:681)
  9. java.base/java.nio.file.Files.write(Files.java:3583)
  10. java.base/java.nio.file.Files.write(Files.java:3633)
  11. eu.ngong.allTxt/eu.ngong.allTxt.App.writeAllTxtSorted(App.java:69)
  12. 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&lt;Path&gt;,因为当前的做法是在try-with-resources中关闭它。

根据文档(重点是我的):

<blockquote>返回的流包含对一个或多个打开目录的引用。这些目录会在关闭流时关闭。

API 注意:
必须在try-with-resources语句或类似的控制结构中使用此方法,以确保流的打开目录在流的操作完成后立即关闭。</blockquote>

  1. static Stream&lt;CharSequence&gt; find() throws IOException {
  2. return Files.walk(Path.of(&quot;C:/Temp&quot;)).map(Path::toString);
  3. }

相反,你可以在writeAllTxtSorted中关闭它。

  1. try (var walk = find().sorted()) {
  2. Files.write(file, walk::iterator, StandardOpenOption.CREATE);
  3. }
英文:

Don't close the Stream&lt;Path&gt; 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>

  1. static Stream&lt;CharSequence&gt; find() throws IOException {
  2. return Files.walk(Path.of(&quot;C:/Temp&quot;)).map(Path::toString);
  3. }

You can close it in writeAllTxtSorted instead.

  1. try (var walk = find().sorted()) {
  2. Files.write(file, walk::iterator, StandardOpenOption.CREATE);
  3. }

huangapple
  • 本文由 发表于 2023年8月5日 00:58:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76837885.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定