aspose file tools
The moose likes Java in General and the fly likes Convert a String into a InputStream Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Convert a String into a InputStream" Watch "Convert a String into a InputStream" New topic
Author

Convert a String into a InputStream

Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 521
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
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

new ByteArrayInputStream(theString.getBytes());


[Jess in Action][AskingGoodQuestions]
Tony Evans
Ranch Hand

Joined: Jun 29, 2002
Posts: 521
So its

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

Thanks Tony
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

Yes.
Neeraj Dheer
Ranch Hand

Joined: Mar 30, 2005
Posts: 225
you can also use StringReader and StringWriter
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24054
    
  13

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

Joined: Jan 30, 2000
Posts: 18670
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 ]

"I'm not back." - Bill Harding, Twister
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert a String into a InputStream
 
Similar Threads
getting the value of .properties file
String to InputStream ?
SOAPBody as a String
A parsing challenge I am stuck
convert from string to inputstream