en….

无非用到的是

1. new Scanner(System.in);

2.new BUfferReader(new InputStreamReader(System.in);

3.System.in.read() //需要转换

当然1,2这两个还有许多坑要注意,比如说 next() 不会读入null字符,就一直等待,nextline() 吸收\n到缓冲区 但是不会读入,readLine 与read()也是一样

这里需要注意:in.nextInt() 接着 in.nextLine()会出现读入空缺现象,很明显,我前面说过,nextLine会吸收\n到缓冲区

贴代码:

 

package IO;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.StringTokenizer;

/**
 * Class for buffered reading int and double values
 */
class Reader解决超时 {
    static BufferedReader reader;
    static StringTokenizer tokenizer;

    /**
     * call this method to initialize reader for InputStream
     */
    static void init(InputStream input) {
        reader = new BufferedReader(
                new InputStreamReader(input));
        tokenizer = new StringTokenizer("");
    }

    /**
     * get next word
     */
    static String next() throws IOException {
        while (!tokenizer.hasMoreTokens()) {
            //TODO add check for eof if necessary
            tokenizer = new StringTokenizer(
                    reader.readLine());
        }
        return tokenizer.nextToken();
    }

    static String nextLine()throws IOException{
        while(!tokenizer.hasMoreElements()){
            tokenizer=new StringTokenizer(
                    reader.readLine()
            );
        }
        return tokenizer.nextToken("\n");
    }

    static int nextInt() throws IOException {
        return Integer.parseInt(next());
    }

    static double nextDouble() throws IOException {
        return Double.parseDouble(next());
    }

    public static void main(String[] args) throws IOException{
        init(System.in);
        StringTokenizer st = new StringTokenizer("this is a test");

        while (st.hasMoreTokens())

        {

            System.out.println(st.nextToken());

        }
       // System.out.println(next());
        System.out.println(nextLine());
    }
}

 

版权声明:本文为dgwblog原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://www.cnblogs.com/dgwblog/p/10047565.html