簡體   English   中英

Java I / O文件程序

[英]Java I/O file program

在以下代碼中,有一個基本的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);
    }}}}

現在,當我嘗試運行它時,出現以下錯誤:

線程“主”中的異常java.lang.Error:未解決的編譯問題:

在Main.main(Main.java:11)

任何想法為什么會發生這種情況?

您有語法錯誤。

花括號太多了。 除去最后一個緊緊的花括號,您就可以了。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM