在下面的代码:关闭输出流'行为不端'?
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输出一些信息到控制台。一旦函数调用结束,我将'i'的值重定向到一个文本文件。这适用于循环的第一次迭代,但只要我调用'outStream.close()',就不会在下一次迭代(控制台或文件)中显示任何输出。有谁知道为什么会发生这种情况?另外什么是解决这个问题的方法?
你为什么要写System.out?你为什么不直接写入文件? – vikingsteve
包含更多代码,以便我们可以看到大图。 – hasan83