gpt4 book ai didi

Java BufferedWriter.write

转载 作者:行者123 更新时间:2023-12-02 12:34:13 28 4
gpt4 key购买 nike

我想将一些输入字符串设置为标准输入,以便另一个进程可以读取标准输入作为输入。我使用 Java BufferedWriter。但是,当输入字符串大于管道大小或其他进程无法读取时,如何处理这种情况?是否有 API 可以检查管道是否可用于写入?

BufferedWriter stdin = new BufferedWriter(new OutputStreamWriter(p1.getOutputStream()));

stdin.write(inputToP2);

stdin.flush();

最佳答案

But how do I handle the case when the input the string is bigger than the pipe size

你不必这样做。它将阻塞,直到有可用空间为止。

or the other process doesn't read?

同样,您将阻塞直到有可用空间。如果其他进程不读取,则表明其他进程中存在错误。

Is there a API to check if the pipe is available for write?

没有。这是 Java 阻塞 I/O。它会阻塞。 Java 不提供任何其他方法来写入进程的输入,或查看阻塞写入是否会阻塞。

I got a code review feedback saying the write will be blocked if no one is reading from it

这是正确的,但是他们希望你对此做什么?它会阻塞 I/O,这就是它的工作原理。询问他们希望您对此采取什么措施。

NB stdin 是输入的名称,而不是输出的名称。不要使用输出流的输入名称。它是另一个进程的输入,但它是您的进程的输出。我也不会称其为 stdout,它不是您的 stdout。实际上,我只是将其称为 out,如果需要更多具体信息,则将其称为 processOut

关于Java BufferedWriter.write,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45202811/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com