How to convert String to InputStream in Java
Published: August 23, 2010 , Updated: August 9, 2011 , Author: mkyong
A simple Java program to convert a String to InputStream, and use BufferedReader to read and display the converted InputStream.
package com.mkyong; 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 str = "This is a String ~ GoGoGo"; // convert String into InputStream InputStream is = new ByteArrayInputStream(str.getBytes()); // read it with BufferedReader BufferedReader br = new BufferedReader(new InputStreamReader(is)); String line; while ((line = br.readLine()) != null) { System.out.println(line); } br.close(); } }
Any Java questions or problems? please post at this JavaNullPointer.com forum, see you there ~