gpt4 book ai didi

java - 流中的数据不完整

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

我正在从进程流中读取数据。基本上,我使用 Runtime.exec API 运行进程并获取进程输入流。

Runtime rt = Runtime.getRuntime();
process = rt.exec("C:/cygwin/home/grec.exe");
InputStream is = process.getInputStream();

然后我将流程的输出收集为 --

            BufferedReader in = new BufferedReader(is);         
char[] ls = new char[s.available()];
ls.read(ls);
String output = new String(ls);

但是,当我打印字符串输出时,我得到了 --

  break [loc]     Add a breakpoint at [file:]line or template
callflow [val] Enable call flow tracing
next Execute the over instruction, stepping over calls
over Execute the current instruction hierarchy

但实际输出是——

  break [loc]     Add a breakpoint at [file:]line or template
callflow [val] Enable call flow tracing
next Execute the over instruction, stepping over calls
over Execute the current instruction hierarchy
print <xpath> Print the value of an XPath expression
profile [val] Turn profiler on or off
reload Reload the script contents
run Restart the script
step Execute the next instruction, stepping into calls
verbose Turn on verbose (-v) output logging
where Show the backtrace of template calls
quit Quit debugger

也就是说,输出的一部分被截断,但代码不会抛出异常。我尝试增加 BufferedReader 的大小并增加数组 ls 的大小。我无法找到此行为的原因。任何有关可能导致此问题的原因的提示或指示都将受到高度赞赏。

最佳答案

使用循环处理所有信息并读取直到流末尾。

Inputstream.available() 没有达到您的预期:

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next invocation of a method for this input stream.

http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#available%28%29

关于java - 流中的数据不完整,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8878731/

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