Java I/O 文件程序

标签 java file input output

在下面的代码中,有一个处理文件的基本 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/

相关文章:

java - 关闭所有浏览器窗口的方法,无论警报如何 Selenium

java - Redhawk 因 Java 错误而崩溃 - 被信号 6、SIGABRT 杀死

java - Spring ,Java : Streaming file download for avoiding out of memory errors

MySQL 使用现有表创建函数语句

java - 尝试捕捉 "Cannot find symbol"

java - 查看和查看组android

java - 如何在不创建文件的情况下打开文件(图像、pdf、utc...)

java - 可靠且快速的文件重复识别方法

javascript - 如何在 Chrome 中检查输入类型 ="date"的(空)值

python - 在 while 循环中从键盘读取字符