【问题标题】:Exception in thread "main" java.lang.IllegalStateException: Scanner closed on File I/O线程“主”java.lang.IllegalStateException 中的异常:扫描程序在文件 I/O 上关闭
【发布时间】:2020-09-13 09:02:29
【问题描述】:

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

线程“main”中的异常 java.lang.IllegalStateException: Scanner closed”。

即使我在int phone input 下方放置了一个scanner.close()。 有什么解释为什么会发生这种情况

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");
    }


    }
}

【问题讨论】:

标签: java exception file-io java.util.scanner java.lang


【解决方案1】:

System.in 上初始化以读取用户输入的 Scanner 没问题。 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();
    }  
}

【讨论】:

  • @Seralathan 嘿,我确实尝试按照您的指示进行操作,但不幸的是,除非我加入 try 括号,否则 pw.close() 无法识别
  • @PhạmQuốcMinhĐăng 您需要 trycatchfinally 块。 x.close()pw.close() 需要放在 finally 块中。您能否分享将 pw.close() 移动到 finally 块后所面临的结果/错误。
  • @PhạmQuốcMinhĐăng 有关关闭 PrintWriter 的推荐方法,请参阅文档 docs.oracle.com/javase/tutorial/essential/exceptions/…
  • pw.close() 无法识别变量(无法解析符号'pw')
  • @PhạmQuốcMinhĐăng pw 变量的范围目前仅在 try 块的范围内。在 try 块之前声明它,以便在块结束后它可用。 PrintWriter pw = null 在尝试关闭 PrintWriter 之前,还要检查 finally 块中的空值。 finally {if (pw != null) {pw.close();}}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-05-07
  • 2018-03-09
  • 1970-01-01
相关资源
最近更新 更多