在下面的代码中,有一个处理文件的基本 I/O 代码:
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws FileNotFoundException {
File file = new File("test.txt");
try {
PrintWriter output = new PrintWriter(file);
output.println("Testing");
output.println("123");
output.close();
} catch (IOException ex) {
System.out.printf("ERROR: %s!\n", ex);
}
try {
Scanner input = new Scanner( file );
String message1 = input.nextLine();
String message2 = input.nextLine();
System.out.println(message1);
System.out.println(message2);
} catch (FileNotFoundException ex) {
System.out.printf("ERROR: %s!\n", ex);
}}}}
现在,当我尝试运行它时,出现以下错误:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
at Main.main(Main.java:11)
知道为什么会发生这种情况吗?
最佳答案
您有语法错误。
右花括号过多。删除最后一个右花括号,就可以开始了。
关于Java I/O 文件程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40096577/