如何清除/重置/打开输入流,以便可以在Java中的2种不同方法中使用它? [英] How to clear/reset/open an input stream so it can be used in 2 different methods in Java?

查看:397
本文介绍了如何清除/重置/打开输入流,以便可以在Java中的2种不同方法中使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

代码如下:

    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
        }
    }

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

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..):

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)

推荐答案

它打印出"omething"的原因是,在对readByte的调用中,您仅消耗了输入的第一个字节.然后readLine消耗流中的其余字节.

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方法的末尾尝试以下行:System.in.skip(System.in.available());

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

这将跳过流中的剩余字节(其他"),同时仍保持流打开以消耗下一个readLine输入.

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

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

Its generally not a good idea to close System.in or 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
    }
}

结果:

$ 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

更新:正如我们在聊天中所讨论的那样,它可以从命令行运行,但不能在netbeans中运行.必须使用IDE.

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

这篇关于如何清除/重置/打开输入流,以便可以在Java中的2种不同方法中使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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