• Post Reply Bookmark Topic Watch Topic
  • New Topic

Difference between System.out.println() and System.err.println()  RSS feed

 
Ranch Hand
Posts: 349
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know that ,

System.out.pritln() will print to the standard out of the system you are using. On the other hand system.err.println() will print to the standard error , but what actually the standard error is and how we can implement this to our project ?


regards,
S
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Normally, when you start your program from a command prompt window (in Windows) or a terminal (in Unix-like operating systems), the standard output and standard error will both print in the command prompt or terminal window, so you will not see a difference.

But they are really two separate streams, and if you want you can redirect them separately. For example, you can let standard output go to the console window, and let standard error go to a file. This Microsoft knowlegde base article explains how to do that on Windows.

 
Sheriff
Posts: 21135
87
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And the good thing is, it works the same on Unix.

That article is missing one thing though. If you use >> instead of > it will not overwrite the destination file but append to it instead.
 
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I use .out and .err when I want to keep error reporting in a different file from my actual program output. For example, if I was trying to print a bunch of address labels, which would be more desireable - a single file looking something like this:

name1
address1

name2
address2

Exception in thread "main" java.lang.NullPointerException
at StataColumn.add(StataColumn.java:32)
at CsvIn.main(CsvIn.java:61)

name4
address4


or getting two files that look like this:

file1:
name1
address1

name2
address2

name4
address4


file2:


Exception in thread "main" java.lang.NullPointerException
at StataColumn.add(StataColumn.java:32)
at CsvIn.main(CsvIn.java:61)

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!