在以下代码中:
for(int i = 5; i <= 100; i+=5)
{
linearSurprise(i); // Function calling 'System.out.print()'
System.setOut(outStream); // Reassigns the "standard" output stream.
System.out.println("Value of i: " + i); // Outputting the value to the .txt file.
outStream.close(); // 'outStrem' is closed so that when I recall my function, output will
// will be sent to the console and not file.
// After debugging, I notice that nothing is being displayed to either the console or file,
// but everything else is still working fine.
}
我正在调用一个函数“linearSurprise”,并在该函数中将一些信息输出到控制台。函数调用结束后,我将“i”的值重定向到文本文件。这适用于循环的第一次迭代,但只要我调用“outStream.close()”,下一次迭代(控制台或文件)中不会显示任何输出。有谁知道为什么会这样?还有什么是解决这个问题的方法?