• Post Reply Bookmark Topic Watch Topic
  • New Topic

Setting System.out  RSS feed

 
Akash Rastu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Just wanted to know, how can we reset the System.out to write back to the standard output stream, once we have changed both the Systems - error and output stream to other streams(say FileOutputStream), using
System.setOut() and System.setErr() methods.

Thanks.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Before you first change the streams, you could store a reference to them somewhere. That way you can change them back later if you need to.
 
Akash Rastu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply.
But what I am expecting is to get/create a reference to the standard outputstream from somewhere else (not stored in my code).
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Akash Rastu:
what I am expecting is to get/create a reference to the standard outputstream from somewhere else (not stored in my code).


Nope. Ulf gives the proper solution. You must save the references before you overwrite them.
 
Akash Rastu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So that means if I have changed System.out and System.err in my code (and have not saved their references), I can no longer write to the screen ( the default outputstream ). In fact no application running on the same JVM can no longer write to the screen.

Please correct me if I am wrong.

Thanks.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right. But the same is true of any other mutable shared resource. If you overwrite the value of a System property, or modify the arguments to main(), or whatever, then those original resources are no longer available.

Regarding the "no other application" -- typically only JVMs running application servers run multiple "applications" at once, and EJBs, servlets, etc, typically don't (and shouldn't) write to standard output. I'm curious: what environment were you thinking about when you mentioned "other applications on the same JVM?"
 
Akash Rastu
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the confirmation.

You were right, I was thinking about other applications deployed in an application Server. When an application changes System.out && System.err, other deployed applications writing to System.out and System.err would also write to the new outputstreams, rather than the default ones.

Thanks again.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Perhaps you are confused because System.err/out/in are mandated to be final by the J2SE API Specification, yet you can alter their value with a call to setErr/Out/In. The explanation here, for simplicity, is that "that is just the way it is". Welcome to Java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!