Java InputStream create from String in UTF-8 encoding

Description

Java InputStream create from String 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  ww  w.ja  v a  2s .c o m

    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;
    }
}



PreviousNext

Related