This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter and FileOutputStream  RSS feed

 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
PrintWriter is used to write data as text into files while FileOutputStream is used to write in binary.

ex: consider


Gives output : (Image Tag somehow not displaying image ... Hence Url )

http://postimg.org/image/dzbugcyv9/

However , If I use FileOutputStream I get binary data which is not readable in text .



gives output :

http://postimg.org/image/nk1hwcoij/


Now , if I use PrintWriter with FileOutputStream then i still get text data as shown below . Why ? How does it become Text and why ?



Output :

http://postimg.org/image/dzbugcyv9/

 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Javadoc of fileoutputstream )]write method says that it writes bytes on the output stream thats why you see bytes in output.
Javadoc of Printwriter print says that it writes character in output stream hence you see characters in output stream.
 
Sushant Kunwara
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But PrintWriter output stream is now FileOutputStream , So ultimately it is still writing bytes ? So , how come we see Text and not Binary in the 3rd example ?
 
Knute Snortum
Sheriff
Posts: 4070
112
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The API of PrintWriter says, "Prints formatted representations of objects to a text-output stream." I'm not sure why you think it would be binary. PrintWriter "wraps" FileOutputStream and converts to text.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!