简体   繁体   English

我的程序跳过了BufferedReader,使其抛出IOException而不提示我输入

[英]My program skips a BufferedReader making it throw an IOException without prompting me for input

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. 我不知道为什么,但是当我添加一个选项来清除程序生成的特定日志文件时,它会跳过程序末尾的BufferedReader,提示用户是重新启动还是终止,并抛出IOException。 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: 记录器类的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.");
    }

}

Your problem is that System.in has already been closed when you try to read from it. 您的问题是,当您尝试从中读取System.in它已被关闭。 It's getting closed by the try-with-resources in clearLog() , since that's what try-with-resources does. 它被clearLog()try-with-resources关闭,因为那是try-with-resources所做的。 If you don't want to do that, you should declare the BufferedReader in clearLog() as a regular variable instead. 如果您不想这样做,则应该在clearLog()中将BufferedReader声明为常规变量。

Or, better yet, you could create a single BufferedReader, and read from it in all your methods, instead of creating new ones all over the place. 或者,更好的是,您可以创建一个BufferedReader,并在所有方法中从中读取,而不是在各处创建新的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM