• Post Reply Bookmark Topic Watch Topic
  • New Topic

Convert a String into a InputStream  RSS feed

 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am looking for code examples for converting a String into a Inputstream.

Thanks for any help

Tony
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
new ByteArrayInputStream(theString.getBytes());
 
Tony Evans
Ranch Hand
Posts: 598
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So its

InputStream is = new ByteArrayInputStream(AString.getBytes());

Thanks Tony
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes.
 
Neeraj Dheer
Ranch Hand
Posts: 225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can also use StringReader and StringWriter
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Neeraj Dheer:
you can also use StringReader and StringWriter


Indeed, you can; but not if you want an InputStream.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that if you're transferring this data between two different machines, it's usually a good idea to specify what encoding is being used, when sending and receiving. E.g.using String's getBytes(String encoding) method. Do not trust the default platform encoding unless you are sure (a) all the machines involved use the same default encoding, and (b) all the characters in your sting are representable using the default encoding. If you only use characters in US-ASCII (Unicode 0-127) there's usually no problem - at least not until you try to print something that uses Microsoft's "smart quotes" (‘foo’ or “bar” ) rather than "straight" quotes ('foo' or "bar").
[ June 01, 2005: Message edited by: Jim Yingst ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!