How To Convert String To InputStream in Java?

Below Java code show how to convert String to InputStream using ByteArrayInputStream class –


/**********************************************************************************
 * Created on 04-2011 Copyright(c) https://kodehelp.com All Rights Reserved.
 **********************************************************************************/
package com.kodehelp.javaio;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;

/**
 * Created by https://kodehelp.com
 * Date: 4/28/11
 */
public class StringToInputStream {
    public static void main(String[] args){
        String text = "Converting String to InputStream Java Example";
        /*
         * Convert String to InputStream using ByteArrayInputStream
         * class. This class constructor takes the string byte array
         * which can be done by calling the getBytes() method.
         */
        try {
            InputStream is = new ByteArrayInputStream(text.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }
}