这是一个模拟InputStream
慢流的模拟:
class SlowInputStream extends InputStream{
private String internal = "5 6\nNext Line\n";
private int position = 0;
@Override
public int available(){
if(position==internal.length()) return 0;
else return 1;
}
@Override
public int read() throws IOException {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
throw new IOException("Sleep Interrupted");
}
if(position==internal.length()) return -1;
return internal.charAt(position++);
}
}
这是测试代码:
Scanner s = new Scanner(new SlowInputStream());
int i=s.nextInt();
System.out.println("i="+i);
int j=s.nextInt();
System.out.println("j="+j);
s.nextLine();
String line = s.nextLine();
System.out.println("line="+line);
s.close();
上述代码的行为是它会停止一段时间并打印三行。什么代码可以输出相同的东西,但在两行之间分割等待时间?