我剛開始使用java中的I/O,請原諒我的懷疑是否愚蠢,但我覺得難以理解這段代碼是如何工作的? 下面代碼中的變量c不會遞增,即使輸入流中只有一個字符,while循環也不會終止,並且輸出流只會連續填充一個字符,但此代碼實際上工作正常,我不明白爲什麼?這段代碼如何工作?
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBytes {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("xanadu.txt");
out = new FileOutputStream("outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
你有和我一樣的懷疑嗎? :) [如何緩衝區字節數組連續填充時,流?](http://stackoverflow.com/questions/18105971/how-the-buffer-byte-array-is-continuously-filling-while-streaming) –