IO System.out & System.err

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

Posts: 53

posted 11 years ago

"err" is for error messages.

"out" is for normal output.

Jeff Albertson

Ranch Hand

Posts: 1780

posted 11 years ago

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

Posts: 83

posted 11 years ago

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

Posts: 56

posted 11 years ago

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

Posts: 1780

posted 11 years ago

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.

There is no emoticon for what I am feeling!

John Dell'Oso

Ranch Hand

Posts: 130

posted 11 years ago

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

Posts: 53

posted 11 years ago

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

Posts: 29

posted 11 years ago

i think u r only here for joy.....................

Kumar Sushil

Greenhorn

Posts: 29

posted 11 years ago

i think u r only here for joy.....................

Jon Egan

Ranch Hand

Posts: 83

posted 11 years ago

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