import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
public class CopyBytes1 {
public static void main(String[] args) throws IOException {
FileInputStream in = null;
FileOutputStream out = null;
try {
in = new FileInputStream("c:\\aaa.txt");
out = new FileOutputStream("c:\\outagain.txt");
int c;
while ((c = in.read()) != -1) {
out.write(c);
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
我将 aaa.txt 保存在 c: 中,但是当我编译它时,它会抛出 FileNotFoundException。为什么会出现这样的情况呢?我应该在哪里保存我的文件?
最佳答案
异常很可能来自
in = new FileInputStream("c:\aaa.txt");
您可以验证是否发布了异常并显示了行号。
确保您的文件不会被意外命名为 aaa.txt.txt,并且仅在 Windows 资源管理器中显示 aaa.txt,因为您隐藏了文件扩展名。
关于java - 以下代码中的文件IO疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4039894/