aspose file tools*
The moose likes Beginning Java and the fly likes System.err or System.out Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.err or System.out" Watch "System.err or System.out" New topic
Author

System.err or System.out

Michelle Lee
Ranch Hand

Joined: Jan 23, 2002
Posts: 42
I have one question:
What is the difference between
System.err.print() or System.out.print().
Both use to print to standard output.
they seems behaviours same.
Can anybody explain to their difference? Thank you!
[ January 22, 2003: Message edited by: michelle Lee ]

Michelle
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
In some applications, output is redirected to a file. In that scenario, System.err.println() is used to display the error messages in the console. If we use System.out.println(), then all the messages will be redirected to the specified file. This is done in order to prevent the error messages being displayed in the redirected file.
------------
Nayan.
Arun Boraiah
Ranch Hand

Joined: Nov 28, 2001
Posts: 233
hi,
Thanks Talukdar ,
I have writen a sample code to test your suggestion hope it will be useful to understand
Type this in the command prompt while executing code.
java ErrStreamCheck >> test.txt
Error free output will go into file 'test.txt' and error records get printed into consol.
public class ErrStreamCheck
{
public static void main(String args[])
{
String strArr[] = { "p1","p2","p3" };
for(int i =0 ; i<5 ; i++ )
{
try
{
System.out.println("array content " + strArr[i] );
}
catch( Exception oEx )
{
System.err.println("Exception due to wrong index.");
}
}
}
}
[ January 22, 2003: Message edited by: Arun Boraiah ]
[ January 22, 2003: Message edited by: Arun Boraiah ]

Sharing is learning
Ken Cobbs
Greenhorn

Joined: Jun 18, 2002
Posts: 29
Thanks Arun,
That example was great! Putting that in my memory bucket( on disk )
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71
Nice example, Arun
I forgot to mention example!!
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
System.err does not print to standard out (as stdout); it prints to standard error (stderr). Normally, these are both the console, but as shown in the above example it doesn't have to be. I think these ideas stem from Unix. At least that's where I have become the most familiar with them.
HTH
Layne


Java API Documentation
The Java Tutorial
Nayanjyoti Talukdar
Ranch Hand

Joined: Feb 12, 2002
Posts: 71

Normally, these are both the console, but as shown in the above example it doesn't have to be.

what do u mean by that? Do u meanit shouldn't behave like that?
--------------
Nayan
Michelle Lee
Ranch Hand

Joined: Jan 23, 2002
Posts: 42
Thank you all! javaranch is really great, you
can learn a lot from here.
 
Consider Paul's rocket mass heater.
 
subject: System.err or System.out