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

InputStream read(byte[] b) buffering input

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

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();

        }

}

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

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

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

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

public int read(byte[] b) throws IOException

Reads some number of bytes from the input stream and stores them into the buffer array b. The number of bytes actually read is returned as an integer. This method blocks until input data is available, end of file is detected, or an exception is thrown.

If the length of b is zero, then no bytes are read and 0 is returned; otherwise, there is an attempt to read at least one byte. If no byte is available because the stream is at the end of the file, the value -1 is returned; otherwise, at least one byte is read and stored into b.

The first byte read is stored into element b[0], the next one into b[1], and so on. The number of bytes read is, at most, equal to the length of b. Let k be the number of bytes actually read; these bytes will be stored in elements b[0] through b[k-1], leaving elements b[k] through b[b.length-1] unaffected.

如果你想立即得到你的输出,你必须在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();

        }

希望它能解决您的问题。