InputStream read(byte [] b)缓冲输入

时间:2015-10-08 11:24:58

标签: java inputstream printwriter

我找到了一个用于java的Fast I / O类,它利用了InpuStream read(byte [] b)方法。这是类:(问题与readInt()有关...可以跳过其余的类详细信息)

import java.io.*;
import java.util.*;

class FastInput
{


        private InputStream stream;
        private byte[] buf = new byte[1024];
        private int curChar;
        private int numChars;


        public FastInput(InputStream userInputStream)
        {
            this.stream = userInputStream;

        }


        public int readChar()
        {

            if (numChars == -1)
                throw new InputMismatchException();

            if (curChar >= numChars)
            {

                curChar = 0;

                try
                {
                    numChars = stream.read(buf);
                } 
                catch (IOException e)
                {
                    throw new InputMismatchException();
                }

                if (numChars <= 0)
                    return -1;

            }

            return buf[curChar++];

        }


        public boolean isSpaceChar(int c) 
        {

                return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;

        }


        public int readInt()
        {

            int c = readChar();
            int sgn = 1;

            while (isSpaceChar(c))
                c = readChar();

            if (c == '-' )
            {
                sgn = -1;
                c = readChar();

            }

            int res = 0;

            do {

                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c - '0';
                c = readChar();

            } while (!isSpaceChar(c));

            return res * sgn;

        }

        public long readLong()
        {

            int c = readChar();
            int sgn = 1;

            while (isSpaceChar(c))
                c = readChar();

            if (c == '-' )
            {
                sgn = -1;
                c = readChar();

            }

            long res = 0;

            do {

                if (c < '0' || c > '9')
                    throw new InputMismatchException();

                res *= 10;
                res += c - '0';
                c = readChar();

            } while (!isSpaceChar(c));

            return res * sgn;

        }



        public String readString() {

            int c = readChar();

            while (isSpaceChar(c))
                c = readChar();

            StringBuilder res = new StringBuilder();

            do
            {

                res.append(c);
                c = readChar();

            } while (!isSpaceChar(c));


            return res.toString();

        }

        // The method created intially skips all whitespaces as defined above and then starts reading everything before \r , \n  , or both
        public String readLine()
        {
                int c=  readChar();

                while (isSpaceChar(c))
                    c = readChar();

                StringBuilder res = new StringBuilder();

                do
                {
                    res.append(c);
                    c = readChar();

                }while (c != '\r' &&  c!= '\n');


                return res.toString();

        }


        public void close()
        {
                try
                {
                    stream.close();
                }
                catch(IOException ex)
                {
                    ex.printStackTrace();

                }
        }



}

现在,我决定使用它。以下是我如何使用它:

import java.io.*;
import java.util.*;

public class Main
{
        public static void main(String[] args)
        {
                FastInput input = new FastInput(System.in);
                PrintWriter output = new PrintWriter(System.out);

                for(int i=0;i<10;++i)
                    output.printf("Hello your num = %d\n" , input.readInt());

                input.close();
                output.close();

        }

}

这两个类都在同一个包中。现在,这就是我输入的内容和我得到的输出:

G:\Java>java Main
1
2
3
4
5
6
7
8
9
10
Hello your num = 1
Hello your num = 2
Hello your num = 3
Hello your num = 4
Hello your num = 5
Hello your num = 6
Hello your num = 7
Hello your num = 8
Hello your num = 9
Hello your num = 10

现在,我的问题是为什么我最终集体获得所有输出?另外,当我停止输入10个数字然后在Byte []数组中停止缓冲时,read方法是如何知道的(如果这是它的表现,我不确定)。

我试图阅读关于它的java文档,但没有提到这件事。

  

public int read(byte [] b)            抛出IOException

     

从输入流中读取一些字节并将其存储到   缓冲阵列b。实际读取的字节数返回为   整数。此方法将阻塞,直到输入数据可用,结束   检测到文件,或抛出异常。

     

如果b的长度为零,则不读取任何字节,返回0;   否则,尝试读取至少一个字节。如果没有字节   是可用的,因为流位于文件的末尾,即值   返回-1;否则,至少读取一个字节并存储到b。

     

读取的第一个字节存储在元素b [0]中,下一个存入元素   b [1],等等。读取的字节数最多等于   b的长度。设k为实际读取的字节数;这些字节   将存储在元素b [0]到b [k-1]中,留下元素b [k]   通过b [b.length-1]不受影响。

1 个答案:

答案 0 :(得分:3)

如果您想立即获得输出,则必须在for循环中添加output.flush()

public static void main(String[] args)
        {
                FastInput input = new FastInput(System.in);
                PrintWriter output = new PrintWriter(System.out);

                for(int i=0;i<10;++i) {
                    output.printf("Hello your num = %d\n" , input.readInt());
                    output.flush();
                }
                input.close();
                output.close();

        }

希望它能解决你的问题。