我不理解爲什麼下面的代碼似乎跳過第一個輸入字符。從輸入流中讀取時缺失第一個字符
import java.io.*;
import java.util.Arrays;
public class Input
{
public static void main (String args[])
{
try {
echo(System.in);
}
catch (IOException e) {
e.printStackTrace();
}
}
static void echo(InputStream stream) throws IOException
{
byte[] input = new byte[10];
char[] inputChar = new char[10];
int current;
while ((current = stream.read()) > 0){
stream.read(input);
int i = 0;
while (input[i] != 0) {
inputChar[i] = (char)input[i];
if (i < 9) {
i++;
}
else {
break;
}
}
System.out.println(Arrays.toString(inputChar));
}
}
}
如果我給輸入爲 「1234567890」 的輸出I得到的是 「[2,3,4,5,6,7,8,9,0,]」 在inputChar的最後一個字符好像是空白。但是,如果它沒有在輸入[9]中讀取任何字節,它應該是空字符(ASCII 0)。如果我給一個長度小於10個字符的輸入,我會在inputChar中的剩餘位置上得到空字符。所以我不確定這裏發生了什麼。
啊,好的。這就說得通了。謝謝。 – Jin
謝謝,完美。這有助於理解爲什麼我總是在[row,col]處得到'ParseError:[1,1]',之後我嘗試解析XML-Result – leole