• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Devaka Cooray
  • Ron McLeod
  • paul wheaton
Saloon Keepers:
  • Tim Moores
  • Piet Souris
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Frits Walraven
  • Scott Selikoff

PrintWriter and FileOutputStream

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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/

 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Sheriff
Posts: 7123
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Could you hold this kitten for a sec? I need to adjust this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic