Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 来自System.in的BufferedReader输入尝试引发异常_Java_Input_Bufferedreader_Inputstreamreader - Fatal编程技术网

Java 来自System.in的BufferedReader输入尝试引发异常

Java 来自System.in的BufferedReader输入尝试引发异常,java,input,bufferedreader,inputstreamreader,Java,Input,Bufferedreader,Inputstreamreader,我正在尝试使用BufferedReader从输入中读取a。它第一次工作,但第二次运行时,我得到一个异常 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.IOExcep

我正在尝试使用BufferedReader从输入中读取a。它第一次工作,但第二次运行时,我得到一个异常

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
它只是在循环中继续运行。我一定错过了什么

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包含在内。

是的,您正在关闭此流:

try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in)))
try with resources语句将关闭块末尾的
BufferedReader
,这将关闭
InputStreamReader
,这将关闭
系统

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

// Deliberately not closing System.in!
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));   
try {
    ...
}
尽管如此,这仍有可能不会像您所希望的那样运行,因为
BufferedReader
可能会消耗(和缓冲)更多的数据。最好创建一次
BufferedReader
(在调用代码中)并将其传递给方法

哦,我建议你扔掉这个:

String str = new String();
根本没有必要。这样会更好:

String str = "";
但即便如此,这也是一项毫无意义的任务。同样,您不需要从
readLine()
返回的字符串中创建新字符串。只需使用:

return br.readLine().trim();

。。。在
块中尝试
。另外,在
catch
块中记录
str
没有意义,因为它将是空的-只有在读取行时才会抛出
IOException

退出定义在哪里?请发布一个可编译程序。@RohitJain其Java7@sanbhat. 它不可编译(在Java 7上)