Java InputStream读取(字节[]b)缓冲输入

Java InputStream读取(字节[]b)缓冲输入,java,inputstream,printwriter,Java,Inputstream,Printwriter,我发现了一个利用InpuStream read(byte[]b)方法的java快速I/O类。下面是类:(问题与readInt()有关……可以跳过类的其余细节) 现在,我的问题是,为什么我最终得到所有的输出?另外,read方法是如何知道何时停止输入10个数字,然后停止在Byte[]数组中的缓冲的(如果这是它的行为,我不确定) 我试着阅读java文档,但没有提到这件事 公共整数读取(字节[]b) 抛出IOException 从输入流中读取一定数量的字节并将其存储到 缓冲区数组b。实际读取的字节数返回

我发现了一个利用InpuStream read(byte[]b)方法的java快速I/O类。下面是类:(问题与readInt()有关……可以跳过类的其余细节)

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

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

公共整数读取(字节[]b) 抛出IOException

从输入流中读取一定数量的字节并将其存储到 缓冲区数组b。实际读取的字节数返回为 整数。此方法阻止,直到输入数据可用,结束 检测到文件,或引发异常

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

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


如果要立即获得输出,必须在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();

        }
publicstaticvoidmain(字符串[]args)
{
快速输入=新的快速输入(系统输入);
PrintWriter输出=新的PrintWriter(System.out);

对于(int i=0;iOkay,我认为这是我新构建的类中的一些问题。我错过了这样一件愚蠢的事情。谢谢你可以将PrintWriter构造为自动刷新:PrintWriter output=new PrintWriter(System.out,true);
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
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();

        }