Convert String To InputStream In Java

This is simple exmaple for converting string object to a InputStream and then read it using the BufferedReader.

package javabeat.net.core;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class StringToInputStreamExample {
	public static void main(String[] args) throws IOException {
		String sampleString = "This is a example for String To InputStream";
	 
		// Convert String into InputStream using getBytes method
		InputStream is = new ByteArrayInputStream(sampleString.getBytes());
	 
		// Read using BufferedReader
		BufferedReader br = new BufferedReader(new InputStreamReader(is));
	 
		String line;
		while ((line = br.readLine()) != null) {
			System.out.println(line);
		}
	 
		br.close();
	   }
}

Leave a Reply

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

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Pin It on Pinterest

Share This

Share this post with your friends!

Share This

Share this post with your friends!