我不确定我的错误在哪里。
public class TestFinalClass {
public static void main(String[] args) throws IoException {
java.io.File file = new java.io.File("Rockstar.txt");
if (file.exists()) {
System.out.println("file already exists");
System.exit(1);
}
java.io.PrintWriter output = new java.io.PrintWriter(file);
output.print("Bob");
output.println("100");
output.print("Bibelo");
output.println("33");
output.close();
}
}
这只是将一些数据写入文件,当我这样做时,我从 eclipse 收到错误消息:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Unhandled exception type FileNotFoundException
at TestFinalClass.main(TestFinalClass.java:9)
最佳答案
java.io.IoException
不是用于捕获 java.io.FileNotFoundException
的有效异常类。
编译时出现以下错误:
---------- Java Compile ----------
test.java:10: error: cannot find symbol
public static void main(String[] args) throws IoException {
^
symbol: class IoException
location: class test
1 error
Output completed (2 sec consumed) - Normal Termination
但是,如果将 IoException
更改为 IOException
,代码就会编译。
此外,请确保正确导入所需的类:
import java.io.IOException;
关于java - IoException 并写入文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20690582/