English 中文(简体)
如何明确/重新确定/开启投入流,以便能够在 Java使用两种不同方法?
原标题:How to clear/reset/open an input stream so it can be used in 2 different methods in Java?

该法典:

    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”自动化,而不是让我再次投入。)

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)
最佳回答

它在“罗马”中印刷的理由是,在你要求阅读Byte时,你只消费了你的第一线投入。 而后读书则消费下游的其余 by。

请将此项目在你读完后处理 方法:System.in.skip(System.in. Available();

这将绕过您的流(“环形”)中剩余的 by,而您的下游仍然愿意为阅读Line消费你的下一个投入。

它一般不是关闭系统的良好想法。 或系统。

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: 正如我们在聊天中所讨论的那样,它从指挥线工作,而不是从净户工作。 必须与民主选举学会合作。

问题回答

暂无回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签