Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Write a String to ByteArrayOutputStream  RSS feed

 
Sara Tracy
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • 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
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
thanks Edwin !
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!