【发布时间】:2013-12-10 17:03:22
【问题描述】:
当我遇到这段代码时,我正在练习在 Java 中使用字节流:
import java.io.*;
public class CopyBytes_practice {
public static void main(String args[]) throws IOException {
FileInputStream f=null;
FileOutputStream fo=null;
int c;
int d;
try {
f=new FileInputStream("a.png");
fo=new FileOutputStream("b.png");
c=f.read();
while(c != -1){
fo.write(c);
}
} finally {
if (f != null) {
f.close();
}
if (fo != null) {
fo.close();
}
}
}
我使用了一个 35kb a.png 文件和一个 0kb b.png 文件来执行代码,但是代码一直运行——在我手动停止 JVM 之前,b.png 的大小达到了 905mb。
我很困惑,为什么没有返回文件结束状态?是不是二进制文件不支持,还是有其他问题?
【问题讨论】: