Feb
09
Posted (Johan Känngård) in Java on 2006-02-09

Arguments in many methods in the Java APIs are of the type InputStream. This is how to get an InputStream from a String, where argument to getBytes, UTF-8, is the encoding to use:

InputStream is = new ByteArrayInputStream(myString.getBytes("UTF-8"));

Technorati Tags:


Comments:
Reza on December 10th, 2006 at 16:51:58 #

Thanks for the code, it helped me a lot.

Juan Zangla on April 30th, 2007 at 22:53:15 #

Thanks for the code. It is very useful.

Ragnar on September 17th, 2007 at 13:18:27 #

I’ve been looking for this code for hours.
Thanks for post it.

Sirish on November 28th, 2007 at 13:19:25 #

Thankx a lot..,
This code is very simple and works excellent.

Once again, Thankx a lot buddy.!

Tej on January 3rd, 2008 at 09:18:39 #

Helped me too !! :)

coco on March 6th, 2008 at 17:03:17 #

thanks, it helps me so much

Steve on April 3rd, 2008 at 10:36:02 #

Code is correct except for the particular overload of GetBytes. You should _always_ specify an encoding when calling GetBytes, e.g. myString.GetBytes(’UTF-8′). The bare form of GetBytes shouldn’t be used.

Johan Känngård on April 3rd, 2008 at 10:46:29 #

@Steve, I am embarrassed to see that I missed that in the code. Thanks for the heads up!

Chris Lawn on April 28th, 2008 at 01:18:30 #

Thanks, really helped with a xml file!

Post a comment
Name: 
Email: 
URL: 
Comments: