IllegalStateException 发生,写入流

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

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

tmpjava.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(&quot;AllTxt&quot; + fileId);
		Files.deleteIfExists(file);
69-&gt;    Files.write(file, find().sorted()::iterator, StandardOpenOption.CREATE);
	} catch (Exception e) {
		err(e, &quot;problems&quot;, l);
	}
}

static Stream&lt;CharSequence&gt; find() throws IOException {
	try (Stream&lt;Path&gt; walk = Files.walk(Path.of(&quot;C:/Temp&quot;))) {
		return walk.map(pth -&gt; 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&lt;Path&gt;,因为当前的做法是在try-with-resources中关闭它。

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

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

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

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

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

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

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>

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

You can close it in writeAllTxtSorted instead.

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

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:

确定