0
public class TestFileStream {
public static void main(String[] args) throws IOException {
FileOutputStream output=new FileOutputStream("temp.dat");
for(int i=1;i<=10;i++){
output.write(i);
}
output.close();
FileInputStream input=new FileInputStream("temp.dat");
while((input.read())!=-1){
System.out.print(input.read()+" ");
}
input.close();
}
}
This is my result 爲什麼結果是2 4 6 8 10? 1 3 5 7 9在哪裏?爲什麼結果是2 4 6 8 10?
你做你的循環雙重閱讀
(input.read())!=-1
指出,以上一次又一次每次迭代;你對結果感到驚訝嗎? – fge從[docs](https://docs.oracle.com/javase/7/docs/api/java/io/FileInputStream.html#read%28%29)中進行讀取,「從此讀取一個字節的數據輸入流,如果還沒有輸入,這個方法會阻塞。「如果你會閱讀文檔,你會得到你的答案。您以字節讀取。 :) –
謝謝!是的,我忽略了這個! –