【问题标题】:My program skips a BufferedReader making it throw an IOException without prompting me for input我的程序跳过了 BufferedReader 使其抛出 IOException 而不提示我输入
【发布时间】:2014-11-14 08:22:39
【问题描述】:

我正在编写一种记录数据并将其导出到外部文件的程序。在我的主课中,如果您想重新启动程序或终止它,我有这个循环会在程序结束时提示您。我不知道为什么,但是当我添加一个选项来清除程序生成的特定日志文件时,它会跳过程序末尾的 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)

对于那些确实想看的人,这里是记录发生的方法:

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

}

【问题讨论】:

  • 当您发布问题时,请创建一个最小示例来说明您的问题。没有人愿意通读您的整个应用程序以找出程序中不工作的部分。您应该让读者尽可能简单,以确保快速响应。
  • 异常的好处是它们(通常)会告诉你出了什么问题。
  • @thatidiotguy 对不起。我更新了代码,所以它只是主类文件。
  • 请发布异常。没有它,堆栈跟踪毫无意义。

标签: java bufferedreader ioexception


【解决方案1】:

您的问题是当您尝试从中读取时,System.in 已经关闭。它正在被clearLog() 中的try-with-resources 关闭,因为这就是try-with-resources 所做的。如果您不想这样做,则应将clearLog() 中的BufferedReader 声明为常规变量。

或者,更好的是,您可以创建一个 BufferedReader,并在所有方法中读取它,而不是到处创建新的。

【讨论】:

    猜你喜欢
    • 2011-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-26
    • 1970-01-01
    相关资源
    最近更新 更多