我正在编写一种记录数据并将其导出到外部文件的程序。在我的主类中,我有这个循环,如果你想重启程序或终止程序,它会在程序结束时提示你。我不知道为什么但是当我添加一个选项来清除程序生成的特定日志文件时,它会跳过程序末尾的BufferedReader,提示用户是重启还是终止并抛出IOException。我的代码出了什么问题?
主要课程:
package com.record.project;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
Scanner input = new Scanner(System.in);
System.out
.println("Display records, enter data, or edit records file? (a , b, c)");
String choice = input.nextLine();
Choice.sort(choice);
try {
Main.loop();
} catch (IOException e) {
e.printStackTrace();
System.out.println("Main main error");
}
System.out.println("Program complete.");
input.close();
}
public static void loop() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Program body complete.");
System.out
.println("Restart program or terminate? (restart, terminate)");
String choice = br.readLine();
switch (choice) {
case "restart":
loopA: while (1 < 2) {
System.out
.println("Display records, enter data, or edit records file? (a , b, c)");
String choice1 = br.readLine();
Choice.sort(choice1);
System.out.println("Program body complete.");
System.out
.println("Restart program or terminate? (restart, terminate)");
String choice2 = br.readLine();
if (choice2.equals("terminate")) {
break loopA;
}
System.out.println("Program terminated.");
break;
}
break;
case "terminate":
System.out.println("Program terminated.");
System.out.println("Program complete.");
System.exit(1);
break;
default:
System.out.println("Invalid answer.");
System.exit(1);
break;
}
}
}
当我运行它时,这是输出: 输入 =用户输入
Display records, enter data, or edit records file? (a , b, c)
*input*clearlog
Clear all logs or specific?(all, specific)
*input*all
Program body complete.
Restart program or terminate? (restart, terminate)
java.io.IOException: Stream closed
Main main error
Program complete.
at java.io.BufferedInputStream.getBufIfOpen(Unknown Source)
at java.io.BufferedInputStream.read(Unknown Source)
at sun.nio.cs.StreamDecoder.readBytes(Unknown Source)
at sun.nio.cs.StreamDecoder.implRead(Unknown Source)
at sun.nio.cs.StreamDecoder.read(Unknown Source)
at java.io.InputStreamReader.read(Unknown Source)
at java.io.BufferedReader.fill(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at com.record.project.Main.loop(Main.java:40)
at com.record.project.Main.main(Main.java:21)
对于那些想要查看的人,这里是记录发生的方法:
记录器类clearLog方法:
@SuppressWarnings("resource")
public static void clearLog() {
System.out.println("Clear all logs or specific?(all, specific)");
try (BufferedReader br = new BufferedReader(new InputStreamReader(
System.in))) {
File fileR = new File("read.log");
File fileW = new File("write.log");
File fileC = new File("clear.log");
BufferedWriter frR = new BufferedWriter(new FileWriter(fileR));
BufferedWriter frW = new BufferedWriter(new FileWriter(fileW));
BufferedWriter frC = new BufferedWriter(new FileWriter(fileC));
String choice = br.readLine();
switch (choice) {
case "all":
frR.write("");
frW.write("");
frC.write("");
break;
case "specific":
System.out.println("Which file? (read, write, clear)");
String fileDel = br.readLine();
switch (fileDel) {
case "read":
frR.write("");
break;
case "write":
frW.write("");
break;
case "clear":
frC.write("");
break;
default:
System.out.println("Invalid answer.");
break;
}
default:
System.out.println("Invalid answer.");
break;
}
} catch (IOException e) {
e.printStackTrace();
System.out.println("Logger error.");
}
}
答案 0 :(得分:2)
您的问题是当您尝试从中读取时System.in
已经关闭。它已被try-with-resources
中的clearLog()
关闭,因为这是try-with-resources
的作用。如果您不想这样做,则应将BufferedReader
中的clearLog()
声明为常规变量。
或者,更好的是,您可以创建一个BufferedReader,并在所有方法中读取它,而不是在整个地方创建新的。