在学习Java输入输出流时,尝试通过Scanner in = new Scanner(new InputSteam())
实现输入,但是发现当调用int a = in.nextInt();
时,程序在InputSteam的read方法中一直循环调用,且如果不调用BufferedInputStream或者Scanner无法实现输入
所以比较好奇,原生Java输入流(BufferedInputStream或者Scanner)是如何实现阻塞式读取控制台中用户输入的数据的?
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
public class Main {
public static void main(String[] args) throws IOException {
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
Scanner in = new Scanner(new InputStream() {
@Override
public int read() throws IOException {
int i=0;
while (i==2){
return 0;
}
return -1;
}
});
int a = in.nextInt();
System.out.println(a);
}
}