It's not a secret anymore!*
The moose likes Java in General and the fly likes diff between System.out and 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 "diff between System.out and System.err" Watch "diff between System.out and System.err" New topic
Author

diff between System.out and System.err

saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
what is the actual difference bwtween system.out.println and system.err.println()


A = HARDWORK B = LUCK/FATE If C=(A+B) then C=SUCCESSFUL IN LIFE else C=FAILURE IN LIFE
SCJP 1.4
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18553
    
  40

Originally posted by saikrishna cinux:
what is the actual difference bwtween system.out.println and system.err.println()


The first writes to "standard output". The second writes to "standard error". These two streams are setup by the operating system when the Java program is executed... And can be redirected with the uses of "|", ">", etc. on the command line.

If the Java program is just ran without any redirection (and the java program don't redirect them), there should be no difference.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

There is neither system.out.println(), nor system.err.println().

Extending Henrys answer,


is guaranteed to print "Hello World", but

might print "llo He World" or "HeWorld llo" too.

[ May 13, 2006: Message edited by: Stefan Wagner ]
[ May 13, 2006: Message edited by: Stefan Wagner ]

http://home.arcor.de/hirnstrom/bewerbung
Shaan Shar
Ranch Hand

Joined: Dec 27, 2005
Posts: 1249

Henery/Stefan,

Could you please eloborate some more about the System.err and System.out .....


It's really sound very much interesting for me.



Stefan Wagner
Ranch Hand

Joined: Jun 02, 2003
Posts: 1923

There isn't much to elaborate on that.

If you got an errormessage you want to put out to console, write it to System.err, and normal output to System.out, so the user can distinguish them.

Assume a program which searches for email-Adresses and a second one, which writes an email to an adress.


You redirect the output to another program as input:

The second program would try to write a mail to "Unknown shortcut: Virginie".
But the user can use redirection:

The message "Unknown shortcut: Virginie" would go to the logfile, and only the valid output would be used as input for the second program.
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Stefan Wagner:
There is neither system.out.println(), nor system.err.println().

Extending Henrys answer,


is guaranteed to print "Hello World", but

might print "llo He World" or "HeWorld llo" too.

[ May 13, 2006: Message edited by: Stefan Wagner ]

[ May 13, 2006: Message edited by: Stefan Wagner ]


No boss i nerver see such thing happen in my system.
it is displaying correctly
the order is correct




cinux
Srinivas Kalvala
Ranch Hand

Joined: Oct 20, 2005
Posts: 257

Originally posted by Ankur Sharma:
Henery/Stefan,

Could you please eloborate some more about the System.err and System.out .....


It's really sound very much interesting for me.






Hai

Its very simple,

Every operating system opens file handles for system input , output and error. the programming languages links your application to that handles.

System.out. --------> output handle ( mostly conslole ).
System.err --------> linked to both err file or some times to console

ofcourse you can change the file handle to which these are pointing.....

read some operation stuff( preferrebly unix)..

All the best....
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[cinux]: No boss i nerver see such thing happen in my system.
it is displaying correctly
the order is correct


The fact that it displays correctly on your system is no guarantee for other systems. On many systems, standard output and standard error will use different buffers, and these buffers may be flushed at different times. On my own system just now I got:

HeWorld
llo


"I'm not back." - Bill Harding, Twister
Guy Allard
Ranch Hand

Joined: Nov 24, 2000
Posts: 776
Generally STDERR is not buffered, STDOUT is.

The last thing STDERR writes do is internally call flush().

If you intermingle the two, e.g.:



it is possible (likely) to see error messages before you see what actually caused the error.

Guy
saikrishna cinux
Ranch Hand

Joined: Apr 16, 2005
Posts: 689
Originally posted by Srinivas Kalvala:




System.out. --------> output handle ( mostly conslole ).
System.err --------> linked to both err file or some times to console



you r saying tht it saves in the error file
where can i get tht file in my system???
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: diff between System.out and System.err