0

For reading input from STDIN i always used Scanner object. Recently I read about BufferedInputStream here that BufferedInputStream is fastest way to read input. In Scanner we have various methods (to read integer nextInt(), to read byte nextByte(), to read string next() etc.) Currently i am reading some strings (at max 100000 characters) and some integers. According to these integers, i further run my for loop which takes input from the STDIN. Below is the code snippet which i use till now-

    Scanner sc = new Scanner(System.in);
    int numOfStr = sc.nextInt();
    String inputStr[] = new String[numOfStr];
        for (int i = 0; i < numOfStr; i++) {
                inputStr[i] = sc.next();
        }
    }

However with BufferedInputStream we have only read() to read input. So how can i differentiate among the inputs? Can somebody please write down the BufferedInputStream code equivalent to the above Scanner ? Should i use StringTokenizer class to tokenize to data which i am getting from read() ? Will this not make the whole stuff (reading input from the STDIN) more slower than Scanner?

4

2 回答 2

1

BufferedInputStream 不解析任何数据,它只是读取数据。您可以将缓冲的阅读器传递给扫描仪类并从那里解析它。

于 2012-03-27T16:54:57.257 回答
0

查看Java 教程中关于流的章节。它描述了不同类型的流、它们的用途和用法。

但是,我不太明白为什么在读取用户输入时读取操作必须快速。与您从键盘读取输入的任何方法相比,用户键入的速度要慢得多(慢几个数量级)。

于 2012-03-27T17:02:41.373 回答