File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes IO System.out  & System.err Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "IO System.out  & System.err" Watch "IO System.out  & System.err" New topic
Author

IO System.out & System.err

amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
hi all,
Why there r 2 objs.--- System.out & System.err ?
by def. both represents Console o/p. In both cases msges. can be redirected to other device. What is the exact diff. bet. these 2 objs?

thanks
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
"err" is for error messages.

"out" is for normal output.
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
I think you are being confused by the fact that both streams *by default* go to the console window. This has nothing to do with Java: as in all processes, these two streams can be redirectly independently, say System.err appends to a log file while System.out is left to output to the console.


There is no emoticon for what I am feeling!
amod gole
Ranch Hand

Joined: Dec 07, 2005
Posts: 83
hi all,
and thanks for reply,
first Joni Salonen i know that System.err is used for error
and System.out is used for output

now try this use
System.err for output
System.out for error(if you know how to redirect error to output stream)


thanks Jeff Albertson for your reply
i am not sure but i think that using System.err we can handle that error
somewhere in other program
if you hava some idea reply me
Dhaval Shah
Ranch Hand

Joined: Jun 10, 2005
Posts: 56
Hi,
can anyone tell me as to where exactly do we use System.err in the application. And if possible can you please explain with an example

Thanks in advance .. . . . .


Thanks and Regards,<br />Dhaval Shah<br />SCJP 1.4<br />SCWCD 1.4
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by gladiator shah:
[QB]can anyone tell me as to where exactly do we use System.err in the application. And if possible can you please explain with an example
/QB]


I don't understand your question. You could use it for error messages, right, if console output is considered appropriate for your application. More generally, Java programmers use logging, for example, log4j.
John Dell'Oso
Ranch Hand

Joined: Apr 08, 2004
Posts: 130
Amod, Gladiator,

Without any redirections in place, err and out will write output to the console by default. Now for example, if you wish to log errors to a file so that you can take a look at them offline for example, you could redirect the err stream to a file and have normal running type messages stream to the console if that's appropriate.

Run this code and see if it makes things any clearer:



You should now see a file called out.txt with two lines written to it. Without the call to the method setErr to redirect the output, these two lines would have been written to the console.

Note this is just an example for demonstration purposes. As Jeff has said, in "real life" you would use a logging API for this sort of thing.

Regards,
JD
Joni Salonen
Ranch Hand

Joined: Jan 07, 2006
Posts: 53
now try this use
System.err for output
System.out for error(if you know how to redirect error to output stream)


What about such use? Seems unnatural to me.
Kumar Sushil
Greenhorn

Joined: Jan 31, 2006
Posts: 29
i think u r only here for joy.....................
Kumar Sushil
Greenhorn

Joined: Jan 31, 2006
Posts: 29
i think u r only here for joy.....................
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Sounded like the OP was asking more about "why" someone would want to do such a thing.

Just consider that it gives you extra flexibility.... you can have two classes of output - by default, they go to the same place, so you never need to differentiate.

But, this feature allows you to separate them easily. Take this example:

1. Write all your code that is printing errors using System.err, write all your code that gives informational messages using System.out.

2. Go on and build an app that makes many, many calls to these two streams, in hundreds of places in the code.

3. Five years (or three weeks) down the line, when you have 800 calls to System.out.println and System.err.println scattered throughout your code, then you get a requirement to monitor your app, and send a page any time an error message is generated.

At this point, you can change all those calls to System.err.println.... or, you can start redirecting that output to a separate file, and set up something to monitor that file. Or, insert a new custom PrintWriter like in John's example, that sends the page AND writes to a file....

The point is, you have a LOT of options, that you wouldn't have if all your messages were going to the same place.


DISCLAIMER: I woud avoid using System.out and System.err in lots of places anyway..... put a wrapper method in some utility class, and call that from everywhere... make the argument list a custom bean of your own type, so you can change the parameters without changing all the calls... now, when you get a requirement to do something different with the output, you make the change in one place....


Also, something else to keep in mind about System.out and System.err.... and I don't know whether this is accurate on every OS, development language, etc.... but they are not synchronized with each other, at least on some platforms. That means, if you write out a big block of text with one, then again with the other, the big blocks can show up intermingled in the output. That's enough to really mess with you when you're trying to read a stack trace....

Hope this helps.
-- Jon
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: IO System.out & System.err
 
Similar Threads
Swimming up Stream
Problem in eclipse IDE Galileo
How to log java.exe errors in bat file
what is the usage of printStackTrace()?
how to peep a java file with multiple error lines page by page in DOS??