简体   繁体   English

关闭输出流“行为异常”?

[英]Closing output stream 'misbehaving'?

In the following code: 在下面的代码中:

    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. 
    }

I am calling a function 'linearSurprise' and in that function I output some information to the console. 我将函数称为“ linearSurprise”,并在该函数中向控制台输出一些信息。 Once the function call is over, I redirect the value of 'i' to a text file. 函数调用结束后,我将'i'的值重定向到文本文件。 This works for the first iteration of the loop, but as soon as I call 'outStream.close()' no output whatsoever is displayed in the next iteration(console or file). 这适用于循环的第一次迭代,但是当我调用'outStream.close()'时,在下一次迭代(控制台或文件)中将不会显示任何输出。 Does anyone know why this is happening? 有人知道为什么会这样吗? Also what would be a workaround to this problem? 另外,解决此问题的方法是什么?

You're closing the OutputStream inside your loop, and System.out is now closed; 您要关闭 OutputStream你的循环里面System.out现在是封闭的; you have to reassign an open OutputStream to it to be able to write any more output. 您必须为其重新分配一个打开的OutputStream才能写入更多输出。

For this purpose, you really ought to be writing directly to the FileOutputStream instead; 为此,您实际上应该直接写到FileOutputStream there's no value in redirecting System.out to it, and it leads to problems like this one. System.out重定向到它没有任何价值,这会导致类似这样的问题。

PrintStream outStream = new PrintStream(File outputFile);
for(int i = 5; i <= 100; i += 5)
{
    linearSurprise(i);
    outStream.println("Value of i: " + i);
}
outStream.close();

This assumption is invalid: 此假设无效:

'outStrem' is closed so that when I recall my function, output will be sent to the console and not file. 'outStrem'已关闭,因此当我调用函数时,输出将发送到控制台而不是文件。

Why would it magically go back to the console? 为什么会神奇地回到控制台? It will just be written to a closed stream instead, which will cause an exception which is swallowed by PrintStream . 而是将其写入封闭的流中,这将导致异常,该异常被PrintStream吞没。

If you want to set it back to the original console stream, you need to do that explicitly: 如果要将其设置回原始控制台流,则需要明确地执行以下操作:

PrintStream originalOutput = System.out;

// Do stuff...

System.setOut(originalOutput); // Now we can write back to the console again

循环后关闭文件

outStream.close();
System.setOut(outStream); // Reassigns the "standard" output stream.
for(int i = 5; i  <= 100; i+=5)
    {
        linearSurprise(i); // Function call


        System.out.println("Value of i: " + i); // Outputting the value to the .txt file.

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. 
    }
    outStream.close(); // 'outStrem' is closed so that when I recall my function, output 

If you close the outStream after you are finished writing to it instead of after one iteration it should work. 如果您在完成写入后关闭outStream,而不是经过一次迭代,则它应该可以工作。

You can try outStream.flush() instead of outStream.close() inside loop. 您可以在循环内尝试使用outStream.flush()而不是outStream.close() It may work. 它可能会起作用。 As outStream.close(); 作为outStream.close(); will close your file. 将关闭您的文件。 it is better to close after loop completion. 最好在循环完成后关闭。

由于您调用CLose(),因此流已被处置,因此不再存在

Print to File as follow: 打印到文件,如下所示:

outStream.println("What ever you want");

And close the stream after the loop. 并在循环后关闭流。

Dont't setOut(outStream); 不要setOut(outStream);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM