如何清除/重置/打开输入流,以便它可以在 Java 中的 2 种不同方法中使用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7680976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:56:52  来源:igfitidea点击:

How to clear/reset/open an input stream so it can be used in 2 different methods in Java?

javainputstreamiostreambufferedreadersystem.in

提问by Petrus K.

Here's the code:

这是代码:

    package testpack;
    import java.io.*;

    public class InputStreamReadDemo {


        private void readByte() throws IOException {
            System.out.print("Enter the byte of data that you want to be read: ");        
            int a = System.in.read();
            System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
            // tried writing System.in.close(); and System.in.reset();
        }
        private void readLine() throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            System.out.print("Enter a line of text: ");
            String res = br.readLine();
            System.out.println("You entered the following line of text: "+res);
            // ... and tried writing br.close();
            // but that messes things up since the input stream gets closed and reset..
        }

        public static void main(String[] args) throws IOException {
            InputStreamReadDemo isrd = new InputStreamReadDemo();
            isrd.readByte();                                                // method 1
            isrd.readLine();                                                // method 2
        }
    }

Here's the output when I run this (and I input "Something" when the first method is called, the second method then auto-reads "omething" for some reason instead of letting me input again..):

这是我运行它时的输出(我在调用第一个方法时输入“Something”,然后第二个方法出于某种原因自动读取“something”而不是让我再次输入..):

run:
Enter the byte of data that you want to be read: Something
The first byte of data that you inputted corresponds to the binary value 1010011 and to the integer value 83.
Enter a line of text: You entered the following line of text: omething
BUILD SUCCESSFUL (total time: 9 seconds)

回答by Mjonir74

The reason it prints out "omething" is that in your call to readByte, you only consume the first byte of your input. Then readLine consumes the remainder of the bytes in the stream.

它打印出“某物”的原因是在您调用 readByte 时,您只使用了输入的第一个字节。然后 readLine 消耗流中剩余的字节。

Try this line at the end of your readByte method: System.in.skip(System.in.available());

在 readByte 方法的末尾尝试这一行: System.in.skip(System.in.available());

That will skip over the remaining bytes in your stream ("omething") while still leaving your stream open to consume your next input for readLine.

这将跳过流中剩余的字节(“某事”),同时仍然保持流打开以使用 readLine 的下一个输入。

Its generally not a good idea to close System.in or System.out.

关闭 System.in 或 System.out 通常不是一个好主意。

import java.io.*;

public class InputStreamReadDemo {


    private void readByte() throws IOException {
        System.out.print("Enter the byte of data that you want to be read: ");        
        int a = System.in.read();
        System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
        System.in.skip(System.in.available());
    }
    private void readLine() throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        System.out.print("Enter a line of text: ");
        String res = br.readLine();
        System.out.println("You entered the following line of text: "+res);
        // ... and tried writing br.close();
        // but that messes things up since the input stream gets closed and reset..
    }

    public static void main(String[] args) throws IOException {
        InputStreamReadDemo isrd = new InputStreamReadDemo();
        isrd.readByte();                                                // method 1
        isrd.readLine();                                                // method 2
    }
}

Results in:

结果是:

$ java InputStreamReadDemo
Enter the byte of data that you want to be read: Something
The first byte of data that you inputted corresponds to the binary value 1010011 and to the integer value 83.
Enter a line of text: Another thing
You entered the following line of text: Another thing

UPDATE:As we discussed in chat, it works from the command line, but not in netbeans. Must be something with the IDE.

更新:正如我们在聊天中所讨论的,它可以从命令行运行,但不能在 netbeans 中运行。必须是 IDE 的东西。