线程“main”java.lang.IllegalStateException中的异常:扫描程序在文件 I/O 上关闭

Exception in thread "main" java.lang.IllegalStateException: Scanner closed on File I/O

提问人:Phạm Quốc Minh Đăng 提问时间:9/13/2020 最后编辑:Taslim OseniPhạm Quốc Minh Đăng 更新时间:9/14/2020 访问量:908

问:

我目前正在制作一个类,允许用户根据用户输入的 ID 编辑行。但是,我遇到了以下错误:

线程“main”java.lang.IllegalStateException中的异常:扫描程序已关闭”。

即使我把一个放在下面。 是否有任何解释为什么会发生这种情况scanner.close()int phone input

package com.company;

import javax.sound.midi.SysexMessage;
import java.io.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;

public class Edit {
    static Scanner x;

    public static void editRecord() throws ParseException {
        Scanner scanner = new Scanner(System.in);
        System.out.println("ENter your ID");
        String ID = scanner.nextLine();
        System.out.println("ENter your new name");
        String name = scanner.nextLine();
        System.out.println("ENter your new EMail");
        String email = scanner.nextLine();
        System.out.println("ENter your new Address");
        String address = scanner.nextLine();
        System.out.println("ENter your new Date");
        String date = scanner.nextLine();
        SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
        Date userDate = format.parse(date);
        System.out.println("ENter your Gender");
        String gender = scanner.nextLine();
        boolean Gender = Boolean.parseBoolean(gender);
        System.out.println("ENter your new phone number");
        int phone = scanner.nextInt();


        String filepath = "leads.csv";
    String tempfile = "temp.csv";
    File oldFile = new File(filepath);
    File newFile = new File(tempfile);
    String ID1 = ""; String name1 = "";String email1="";String address1 = ""; String userdate1 = "";
    String Gender1 = ""; String phone1 = "";
    scanner.close();
    try{
        FileWriter fw = new FileWriter(tempfile,true);
        BufferedWriter bw = new BufferedWriter(fw);
        PrintWriter pw = new PrintWriter(bw);
        x = new Scanner(new File(filepath));
        x.useDelimiter("[,\n]");

        while (x.hasNext()){
            ID1 = x.next();
            name1 = x.next();
            phone1 = x.next();
            email1 = x.next();
            address1 = x.next();
            userdate1 = x.next();
            Gender1 = x.next();
            if(ID1.equals(ID)){
                pw.println(name+","+phone+","+email+","+address+","+userDate+","+Gender);
            }
            else {
                pw.println(name1+","+phone1+","+email1+","+address1+","+userdate1+","+Gender1);
            }
            x.close();
            pw.flush();
            pw.close();
            oldFile.delete();
            File dump = new File(filepath);
            newFile.renameTo(dump);
        }


    } catch (IOException e) {
        e.printStackTrace();
        System.out.println("Error Occured");
    }


    }
}
异常 文件-io java.util.scanner java

评论

0赞 Umair Mubeen 9/13/2020
看到这篇文章,希望它能解决你的问题 stackoverflow.com/questions/28052519/......

答:

1赞 Seralahthan 9/13/2020 #1

System.in 上初始化的扫描仪读取用户输入是正常的。 IllegalStateException 是从 try 块中初始化的扫描程序引发的。

当您在 while 循环中关闭 Scanner 时,将引发异常。 为了避免这种情况,将 x.close() 和 pw.close() 语句移动到 finally 块,如下所示:

PrintWriter pw = null;
try {
    FileWriter fw = new FileWriter(tempfile,true);
    BufferedWriter bw = new BufferedWriter(fw);
    pw = new PrintWriter(bw);
    x = new Scanner(new File(filepath));
    x.useDelimiter("[,\n]");

    while (x.hasNext()){
        ...
        pw.flush();
    }

} catch (IOException e) {
    e.printStackTrace();
    System.out.println("Error Occured");
} finally {
    x.close();
    if (pw != null) {
        pw.close();
    }  
}

评论

0赞 Phạm Quốc Minh Đăng 9/14/2020
@Seralathan 嘿,我确实尝试按照您的指示进行操作,但不幸的是,除非我加入 try 括号,否则 pw.close() 无法识别
0赞 Seralahthan 9/14/2020
@PhạmQuốcMinh Đăng 你需要尝试捕捉最后的阻止。x.close() 和 pw.close() 需要放在 finally 块中。您能否分享将 pw.close() 移动到最终块后面临的结果/错误。
0赞 Seralahthan 9/14/2020
@PhạmQuốcMinh Đăng 请参阅文档 docs.oracle.com/javase/tutorial/essential/exceptions/...,了解关闭 PrintWriter 的推荐方法。
0赞 Phạm Quốc Minh Đăng 9/14/2020
pw.close() 无法识别变量(无法解析符号“pw”)
0赞 Seralahthan 9/14/2020
@PhạmQuốcMinh Đăng pw 变量的作用域目前仅在 try 块的作用域内。在 try 块之前声明它,以便在块结束后可用。 此外,在尝试关闭 PrintWriter 之前,请检查 finally 块中的 null 值。PrintWriter pw = null finally { if (pw != null) { pw.close(); } }