• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating InputStream from a String

 
Kalichar Rangantittu
Ranch Hand
Posts: 240
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can I create an instance of an InputStream from a String?

String s = "Hello";

InputStream in = new ..(s);

I cannot use Reader.

Thanks.
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
See ByteArrayInputStream( string.getBytes() ) I bet it does what you need.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes - but beware that uses your platform's default encoding, which may or may not be appropriate, depending what the InputStream is used for. So you may want to specifiy a particular encoding, e.g.

to use UTF-8 encoding.
 
Er. Ashish Aggarwal
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To Convert String from input Stream simple way is


and to convert string into a specified format so we can provide the charset as


for futher reference to convert string from input stream and input stream to string refer link.
http://iandjava.blogspot.in/2013/06/package-com.html
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!