简体   繁体   English

在java中将标准输出流设置为默认值

[英]Setting standard output stream to default in java

I went through the java API documentation.I fiddled with some of the functions and classes and found something strange.The following code I wrote is not setting the standard output stream to the default(System.out). 我浏览了java API文档。我摆弄了一些函数和类,发现了一些奇怪的东西。我写的以下代码并没有将标准输出流设置为默认值(System.out)。 At first,I set the standard output destination to a file and then I tried resetting it but it continues to write in the file. 首先,我将标准输出目标设置为一个文件,然后我尝试重置它,但它继续写入文件。

try
    {

        file=new FileOutputStream("mamamia.txt",true);
        //j=new BufferedOutputStream(file);
        j=new PrintStream (file);
        //byte[] b={49,50,44,67,44,23,57,32};
        //j.write(b,0,5);
        System.setOut(j);
        System.out.println("hey mama mia");
        System.out.println("hello gigs");


        PrintStream j1=new PrintStream (System.out);
        System.setOut(j1);
        System.out.println("Hey bro");

        j.flush();
        file.flush();
        file.close();
         j.close();
    }

'Hey bro' is getting printed in the file.Why is it not going back to default? '嘿兄弟'正在打印在文件中。为什么它不会恢复到默认状态? Prinstream constructor looks for type OutputStream.Since PrintStream is an indirect subclass of OutputStream,I passed System.out directly.It is not showing me any error,the only prob is that it is not going back to the default. Prinstream构造函数查找类型OutputStream.Since PrintStream是OutputStream的间接子类,我直接传递了System.out。它没有向我显示任何错误,唯一的问题是它没有回到默认值。 Can someone help? 有人可以帮忙吗?

What you are doing is, in great simplification: 你正在做的是,非常简单:

stdout = file;
stdout = stdout;

so no surprise it's not set to the original. 所以毫不奇怪它不是原来的。

You would need to keep the original standard output stream in a temporary variable and then assign it back to System.out . 您需要将原始标准输出流保存在临时变量中,然后将其分配回System.out Something like this: 像这样的东西:

PrintStream originalStdout = System.out;
System.setOut(j);
// do your printing...
// and revert
System.setOut(originalStdout);

System.setOut(j1);

Here you are setting System.out to the printstream it already is linked to, which is the file one. 在这里,您将System.out设置为已链接到的打印流,即文件一。

You should first store the original System.out printstream, then reaffect this original printstream once you're done, like : 您应该首先存储原始System.out打印流,然后在完成后重新生成此原始打印流,例如:

try
    {

        file=new FileOutputStream("mamamia.txt",true);
        //j=new BufferedOutputStream(file);
        j=new PrintStream (file);
        //byte[] b={49,50,44,67,44,23,57,32};
        //j.write(b,0,5);
        PrintStream originalStream = System.out;
        System.setOut(j);
        System.out.println("hey mama mia");
        System.out.println("hello gigs");


        System.setOut(originalStream );
        System.out.println("Hey bro");

        j.flush();
        file.flush();
        file.close();
         j.close();
    }

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

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