Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Java PrintStream significance and where to use this?  RSS feed

 
sashwat kumar padhy
Greenhorn
Posts: 20
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I found many programmers using PrintStream in their codes. I looked for this,but failed to get the clear idea of its significance,Why to use it?

"Unlike other output streams, a PrintStream never throws an IOException and the data is flushed to a file automatically i.e. the flush method is automatically invoked after a byte array is written."

So what's the benefit if flush is automatically invoked?

private PrintStream x = null;

Now what could be the possible reasons if someone declares x like this?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, and welcome to JavaRanch!

System.out and System.err are PrintStreams, so there's a pretty big use of them there. Automatically flushing may hurt performance, but it eases use and reduces one possible error. There are places where that tradeoff is worth it. I agree that there's no use to declaring a field and making null if it stays null. Maybe there's a setter for it in the class?
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!