[英]Incomplete data from a stream
我正在從流程流中讀取數據。 基本上,我使用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的大小。我很遺憾找到這種行為的原因。 關於可能是什么原因的任何提示或指示都將受到高度贊賞。
你錯誤地假設available()
告訴你實際可用的字節/字符數。 事實上,它告訴你至少有很多可用......而且它可能是錯誤的,因為它是一個估計。
然后,在將它包裝在BufferedReader
之后,通過訪問InputStream
來復合問題。 因此,您無需獲取可從讀取器讀取的字符數,而是獲取尚未讀入緩沖區的可用字節數。
你可能最好只使用readLine()
一次從BufferedReader
讀取一行。
使用循環處理所有信息並讀取直到流的末尾。
Inputstream.available()沒有按預期執行:
返回可以從此輸入流中讀取(或跳過)的字節數的估計值 , 而不會被下一次調用此輸入流的方法阻塞 。
http://docs.oracle.com/javase/6/docs/api/java/io/InputStream.html#available%28%29
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.