Java String convert to InputStream in UTF-8 encoding
//package com.demo2s; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.io.UnsupportedEncodingException; public class Main { public static void main(String[] argv) throws Exception { String str = "demo2s.com demo2s.com"; System.out.println(toUTF8InputStream(str)); }/*from w w w .ja va 2 s. com*/ public static InputStream toUTF8InputStream(String str) { InputStream is = null; try { is = new ByteArrayInputStream(str.getBytes("UTF-8")); } catch (UnsupportedEncodingException e) { // UTF-8 should always be supported throw new AssertionError(); } return is; } }