• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Write a String to ByteArrayOutputStream

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I know how to read data from a ServletInputStream (or a FileInputStream) and write it to ByteArrayOutputStream.
Example:


However, I would like to read a String in an InputStream and write it to a ByteArrayOutputStream ? For example:
String data = "<?xml version=\"1.0\"?>";
How do I do that ?

thanks
Sara
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if you want to read a String from the InputStream then, wrap the InputStream into a BufferedReader.

You can somewhat like this:



Now, if you want to write your String into a ByteArrayOutputStream or any other kind of OutputStream you could simple use the String.getBytes() method.

For instance;



I hope this helps!
 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Edwin !
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic