等价于cin.tie0在Java中吗?

问题描述

是否只有在程序完成所有输入后才可以打印输出?换句话说,Java中是否有与cin.tie(0)等效的东西?

在C ++中,当我们包含cin.tie(0)时,我们解开std::coutstd::cin。通过这样做,我们确保在输入由给出后,输出不会立即刷新到控制台上用户,而是在用户完成所有输入后刷新输出

考虑一个程序有3个测试用例作为输入的情况。我们的程序将输出Hello World!

输入

Test Case 1
Test Case 2
Test Case 3

不使用cin.tie(0): 程序(c ++)

int main(){
   int t;//holds the info of number of test cases
   cin>>t; 
   for(int i=0;i<t;i++){
      string s; 
      cin >>s; 
      cout<<"Hello World";
   }
   return 0;
}

控制台:

Test Case 1
Hello World!
Test Case 2
Hello World!
Test Case 3
Hello World!

使用cin.tie(0):

int main(){
   ios_base::sync_with_stdio(false); 
   cin.tie(0);
   int t;//holds the info of number of test cases
   cin>>t; 
   for(int i=0;i<t;i++){
      string s; 
      cin >>s; 
      cout<<"Hello World";
   }
   return 0;
}
Test Case 1
Test Case 2
Test Case 3
Hello World!
Hello World!
Hello World!

有什么方法可以在Java中复制cin.tie(0)

解决方法

某些C库(尤其是including glibc)在从某些输入流中读取时会刷新标准输出。您必须使用特定于平台的API调用来更改该行为(可能是通过完全缓冲输入流);我不知道该功能有任何标准/预制包装。