What's the fastest way to read from System.in in Java?

前端 未结 9 2109
梦如初夏
梦如初夏 2020-11-30 19:17

I am reading bunch of integers separated by space or newlines from the standard in using Scanner(System.in).

Is there any faster way of doing this in Ja

9条回答
  •  醉话见心
    2020-11-30 20:04

    In programming perspective this customized Scan and Print class is way better than Java inbuilt Scanner and BufferedReader classes.

    import java.io.InputStream;
    import java.util.InputMismatchException;
    import java.io.IOException;
    
    public class Scan
    {
    
    private byte[] buf = new byte[1024];
    
    private int total;
    private int index;
    private InputStream in;
    
    public Scan()
    {
        in = System.in;
    }
    
    public int scan() throws IOException
    {
    
        if(total < 0)
            throw new InputMismatchException();
    
        if(index >= total)
        {
            index = 0;
            total = in.read(buf);
            if(total <= 0)
                return -1;
        }
    
        return buf[index++];
    }
    
    
    public int scanInt() throws IOException
    {
    
        int integer = 0;
    
        int n = scan();
    
        while(isWhiteSpace(n))   /*  remove starting white spaces   */
            n = scan();
    
        int neg = 1;
        if(n == '-')
        {
            neg = -1;
            n = scan();
        }
    
        while(!isWhiteSpace(n))
        {
    
            if(n >= '0' && n <= '9')
            {
                integer *= 10;
                integer += n-'0';
                n = scan();
            }
            else
                throw new InputMismatchException();
        }
    
        return neg*integer;
    }
    
    
    public String scanString()throws IOException
    {
        StringBuilder sb = new StringBuilder();
    
        int n = scan();
    
        while(isWhiteSpace(n))
            n = scan();
    
        while(!isWhiteSpace(n))
        {
            sb.append((char)n);
            n = scan();
        }
    
        return sb.toString();
    }
    
    
    public double scanDouble()throws IOException
    {
        double doub=0;
        int n=scan();
        while(isWhiteSpace(n))
        n=scan();
        int neg=1;
        if(n=='-')
        {
            neg=-1;
            n=scan();
        }
        while(!isWhiteSpace(n)&& n != '.')
        {
            if(n>='0'&&n<='9')
            {
                doub*=10;
                doub+=n-'0';
                n=scan();
            }
            else throw new InputMismatchException();
        }
        if(n=='.')
        {
            n=scan();
            double temp=1;
            while(!isWhiteSpace(n))
            {
                if(n>='0'&&n<='9')
                {
                    temp/=10;
                    doub+=(n-'0')*temp;
                    n=scan();
                }
                else throw new InputMismatchException();
            }
        }
        return doub*neg;
    }
    
    public boolean isWhiteSpace(int n)
    {
        if(n == ' ' || n == '\n' || n == '\r' || n == '\t' || n == -1)
            return true;
    
        return false;
    }
    
    public void close()throws IOException
    {
        in.close();
    }
    }
    

    And the customized Print class can be as follows

    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.OutputStreamWriter;
    
    public class Print
    {
    private BufferedWriter bw;
    
    public Print()
    {
        this.bw = new BufferedWriter(new OutputStreamWriter(System.out));
    }
    
    
    public void print(Object object)throws IOException
    {
        bw.append("" + object);
    }
    
    public void println(Object object)throws IOException
    {
        print(object);
        bw.append("\n");
    }
    
    
    public void close()throws IOException
    {
        bw.close();
    }
    
    }
    

提交回复
热议问题