来自System.in的BufferedReader输入尝试引发异常 [英] BufferedReader input attempt from System.in throwing exceptions

查看:89
本文介绍了来自System.in的BufferedReader输入尝试引发异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BufferedReader从输入中读取a.第一次运行,但是第二次运行,我得到一个例外.

I am trying to read a from the input using BufferedReader. It works the first time but the second time it is ran I get an exception.

john@fekete:~/devel/java/pricecalc$ java frontend.CUI 
> gsdfgd
Invalid command!
> I/O Error getting string:  java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string:  java.io.IOException: Stream closed
I/O Error: java.io.IOException: java.io.IOException: Stream closed
> I/O Error getting string:  java.io.IOException: Stream closed

它只是一直循环运行.我一定错过了什么.

It just keeps running with that in a loop. I must have missed something.

public static void main(String args[]) {
            if (args.length == 0) {
                    while (!exit) {
                            try {
                                    exit = processLine(commandLine());
                            } catch (IOException e) {
                                    System.out.println("I/O Error: " + e);
                            }
                    }
                    System.out.println("Bye!");
            } else if (args.length == 1) {
                    String line = new String(args[0]);
                    processLine(line);
            } else {
                    String line = new String(args[0]);
                    for (String np : args) {
                            line = new String(line + " " + np); 
                    }
                    processLine(line);
            }
    }

    static private String commandLine() throws IOException {
            String str = new String();
            try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
                    System.out.print("> ");
                    str = new String(br.readLine());
                    str = str.trim();
            } catch (IOException e) {
                    System.out.println("I/O Error getting string: "+ str + " " + e);
                    throw new IOException(e);
            }

            return str;
    }

这真的似乎是关于commandLine()不能正常工作的,所以我只包含了它和main.

It really all seems to be about commandLine() not working so I've just included that and main.

推荐答案

是的,您要在此处关闭流:

Yes, you're closing the stream here:

try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))

该try-with-resources语句将在块的末尾关闭BufferedReader,这将关闭InputStreamReader,这将关闭System.in.

That try-with-resources statement will close the BufferedReader at the end of the block, which will close the InputStreamReader, which will close System.in.

在这种情况下,您不想这样做,所以只需使用:

You don't want to do that in this case, so just use:

// Deliberately not closing System.in!
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
try {
    ...
}

由于BufferedReader可能会消耗(并缓冲)更多数据,因此它仍然可能表现得不尽如人意.您最好在调用代码中创建BufferedReader 一次(并将其传递给方法).

It's still possible that this won't quite behave as you want though, as the BufferedReader could potentially consume (and buffer) more data. You'd be better off creating the BufferedReader once (in the calling code) and passing it to the method.

哦,我建议您摆脱这一点:

Oh, and I suggest you get rid of this:

String str = new String();

完全没有必要.这样会更好:

There's no need for it at all. This would be better:

String str = "";

但是即使那样,这也是毫无意义的.同样,您无需根据readLine()返回的字符串创建一个新的String.只需使用:

But even then, it's a pointless assignment. Likewise you don't need to create a new String from the one returned by readLine(). Just use:

return br.readLine().trim();

块中的

....另外,在catch块中记录str毫无意义,因为它将为空-IOException仅在读取该行时才会抛出...

... within the try block. Also, there's no point in logging str within the catch block, as it's going to be empty - the IOException will only be thrown when reading the line...

这篇关于来自System.in的BufferedReader输入尝试引发异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆