Exception in thread “main” java.lang.IllegalStateException: 文件 I/O 中的扫描器已关闭

huangapple go评论106阅读模式
英文:

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

问题

  1. 我目前正在制作一个类允许用户根据用户输入的ID编辑行但是我遇到了以下错误
  2. >异常线程"main" java.lang.IllegalStateException: Scanner已关闭"
  3. 即使我在```int phone input```下面放了```scanner.close()```
  4. 是否有任何解释为什么会发生这种情况
  5. package com.company;
  6. import javax.sound.midi.SysexMessage;
  7. import java.io.*;
  8. import java.text.ParseException;
  9. import java.text.SimpleDateFormat;
  10. import java.util.Date;
  11. import java.util.Scanner;
  12. public class Edit {
  13. static Scanner x;
  14. public static void editRecord() throws ParseException {
  15. Scanner scanner = new Scanner(System.in);
  16. System.out.println("输入您的ID");
  17. String ID = scanner.nextLine();
  18. System.out.println("输入您的新姓名");
  19. String name = scanner.nextLine();
  20. System.out.println("输入您的新电子邮件");
  21. String email = scanner.nextLine();
  22. System.out.println("输入您的新地址");
  23. String address = scanner.nextLine();
  24. System.out.println("输入您的新日期");
  25. String date = scanner.nextLine();
  26. SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
  27. Date userDate = format.parse(date);
  28. System.out.println("输入您的性别");
  29. String gender = scanner.nextLine();
  30. boolean Gender = Boolean.parseBoolean(gender);
  31. System.out.println("输入您的新电话号码");
  32. int phone = scanner.nextInt();
  33. String filepath = "leads.csv";
  34. String tempfile = "temp.csv";
  35. File oldFile = new File(filepath);
  36. File newFile = new File(tempfile);
  37. String ID1 = ""; String name1 = ""; String email1=""; String address1 = ""; String userdate1 = "";
  38. String Gender1 = ""; String phone1 = "";
  39. scanner.close();
  40. try{
  41. FileWriter fw = new FileWriter(tempfile,true);
  42. BufferedWriter bw = new BufferedWriter(fw);
  43. PrintWriter pw = new PrintWriter(bw);
  44. x = new Scanner(new File(filepath));
  45. x.useDelimiter("[,\n]");
  46. while (x.hasNext()){
  47. ID1 = x.next();
  48. name1 = x.next();
  49. phone1 = x.next();
  50. email1 = x.next();
  51. address1 = x.next();
  52. userdate1 = x.next();
  53. Gender1 = x.next();
  54. if(ID1.equals(ID)){
  55. pw.println(name+","+phone+","+email+","+address+","+userDate+","+Gender);
  56. }
  57. else {
  58. pw.println(name1+","+phone1+","+email1+","+address1+","+userdate1+","+Gender1);
  59. }
  60. x.close();
  61. pw.flush();
  62. pw.close();
  63. oldFile.delete();
  64. File dump = new File(filepath);
  65. newFile.renameTo(dump);
  66. }
  67. } catch (IOException e) {
  68. e.printStackTrace();
  69. System.out.println("发生错误");
  70. }
  71. }
  72. }
英文:

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:

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

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

  1. package com.company;
  2. import javax.sound.midi.SysexMessage;
  3. import java.io.*;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6. import java.util.Date;
  7. import java.util.Scanner;
  8. public class Edit {
  9. static Scanner x;
  10. public static void editRecord() throws ParseException {
  11. Scanner scanner = new Scanner(System.in);
  12. System.out.println("ENter your ID");
  13. String ID = scanner.nextLine();
  14. System.out.println("ENter your new name");
  15. String name = scanner.nextLine();
  16. System.out.println("ENter your new EMail");
  17. String email = scanner.nextLine();
  18. System.out.println("ENter your new Address");
  19. String address = scanner.nextLine();
  20. System.out.println("ENter your new Date");
  21. String date = scanner.nextLine();
  22. SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy");
  23. Date userDate = format.parse(date);
  24. System.out.println("ENter your Gender");
  25. String gender = scanner.nextLine();
  26. boolean Gender = Boolean.parseBoolean(gender);
  27. System.out.println("ENter your new phone number");
  28. int phone = scanner.nextInt();
  29. String filepath = "leads.csv";
  30. String tempfile = "temp.csv";
  31. File oldFile = new File(filepath);
  32. File newFile = new File(tempfile);
  33. String ID1 = ""; String name1 = "";String email1="";String address1 = ""; String userdate1 = "";
  34. String Gender1 = ""; String phone1 = "";
  35. scanner.close();
  36. try{
  37. FileWriter fw = new FileWriter(tempfile,true);
  38. BufferedWriter bw = new BufferedWriter(fw);
  39. PrintWriter pw = new PrintWriter(bw);
  40. x = new Scanner(new File(filepath));
  41. x.useDelimiter("[,\n]");
  42. while (x.hasNext()){
  43. ID1 = x.next();
  44. name1 = x.next();
  45. phone1 = x.next();
  46. email1 = x.next();
  47. address1 = x.next();
  48. userdate1 = x.next();
  49. Gender1 = x.next();
  50. if(ID1.equals(ID)){
  51. pw.println(name+","+phone+","+email+","+address+","+userDate+","+Gender);
  52. }
  53. else {
  54. pw.println(name1+","+phone1+","+email1+","+address1+","+userdate1+","+Gender1);
  55. }
  56. x.close();
  57. pw.flush();
  58. pw.close();
  59. oldFile.delete();
  60. File dump = new File(filepath);
  61. newFile.renameTo(dump);
  62. }
  63. } catch (IOException e) {
  64. e.printStackTrace();
  65. System.out.println("Error Occured");
  66. }
  67. }
  68. }

答案1

得分: 1

  1. 初始化在 **System.in** 上的 **Scanner** 用于读取用户输入是没问题的。
  2. **IllegalStateException** 异常是从在 **try 块** 内初始化的扫描器中抛出的。
  3. 异常被抛出是因为你正在 while 循环内关闭了 Scanner
  4. 为了避免这个问题,将 **x.close()** **pw.close()** 语句移到 **finally 块** 中,如下所示:
  5. ```java
  6. PrintWriter pw = null;
  7. try {
  8. FileWriter fw = new FileWriter(tempfile,true);
  9. BufferedWriter bw = new BufferedWriter(fw);
  10. pw = new PrintWriter(bw);
  11. x = new Scanner(new File(filepath));
  12. x.useDelimiter("[,\n]");
  13. while (x.hasNext()){
  14. ...
  15. pw.flush();
  16. }
  17. } catch (IOException e) {
  18. e.printStackTrace();
  19. System.out.println("发生错误");
  20. } finally {
  21. x.close();
  22. if (pw != null) {
  23. pw.close();
  24. }
  25. }
  1. <details>
  2. <summary>英文:</summary>
  3. The **Scanner** initialized over **System.in** to read the user input is fine.
  4. The **IllegalStateException** is thrown from the scanner initialized within the **try block**.
  5. The exception is thrown as you are closing the Scanner within the while loop.
  6. 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]");

  1. while (x.hasNext()){
  2. ...
  3. pw.flush();
  4. }

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

  1. </details>

huangapple
  • 本文由 发表于 2020年9月13日 17:02:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/63868968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定