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

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.