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("C:\\int.txt");
out = new FileOutputStream("C:\\out.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
这段代码中的数字(-1)来自哪里?
while ((c = in.read()) != -1) {
out.write(c);.
我尝试查看 Java 教程,但它只给了我一个令人困惑的图表。
编辑:我将-1的值更改为-4,这导致最后一个字符被写入很多次。这是为什么?
最佳答案
-1
值标记已到达文件末尾,没有更多内容可读取。
Here是该方法的 javadoc。
关于java - 字节流中字符的数字索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16765170/