I'm writing a sort of program that records data and exports it to an external file. In my main class, I have this loop that prompts you at the end of the program if you want to restart the program or terminate it. I don't know why though but when I added a choice to clear a particular log file generated by the program, it skips the BufferedReader at the end of the program that prompts the user whether to restart or terminate and throws an IOException instead. What's wrong with my code?
Main class:
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;
}
}
}
When I run it, this is the output: input = user input
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)
For those that do want to see, here is the method where the Logging takes place:
Logger class clearLog method:
@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.");
}
}