• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

IO System.out & System.err

 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"err" is for error messages.

"out" is for normal output.
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
amod gole
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .. . . . .
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 130
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think u r only here for joy.....................
 
Kumar Sushil
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i think u r only here for joy.....................
 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
reply
    Bookmark Topic Watch Topic
  • New Topic