线程"main"中的异常java.lang.IllegalStateException:扫描程序在文件I/O上关闭 [英] Exception in thread "main" java.lang.IllegalStateException: Scanner closed on File I/O

查看:39
本文介绍了线程"main"中的异常java.lang.IllegalStateException:扫描程序在文件I/O上关闭的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently making a class that allows users to edit the line based on the ID that the user enters. However, I'm having the error below:

线程"main"中的异常;java.lang.IllegalStateException:扫描仪已关闭".

Exception in thread "main" java.lang.IllegalStateException: Scanner closed".

即使我已将 scanner.close()放在 int电话输入下方.有什么解释为什么会发生

Even though I have put a scanner.close() below the int phone input. Is there any explanation on why is this happening

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


    }
}

推荐答案

通过 System.in 初始化以读取用户输入的 Scanner 很好.从在 try块中初始化的扫描程序抛出 IllegalStateException .

The Scanner initialized over System.in to read the user input is fine. The IllegalStateException is thrown from the scanner initialized within the try block.

在while循环中关闭扫描器时会引发异常.为避免这种情况,将 x.close() pw.close()语句移动到 finally块,如下所示,

The exception is thrown as you are closing the Scanner within the while loop. To avoid it move the x.close() and pw.close() statements to the finally block as below,

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

这篇关于线程"main"中的异常java.lang.IllegalStateException:扫描程序在文件I/O上关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆