Searching...
Thursday, 17 October 2013

How to convert Java String to InputStream ?

11:00

InputStream is a super class of all classes representing an input stream of bytes. Here is one example to convert a String to InputStream.

Example

package com.pretech;
import java.io.*;
public class StringtoInputStream {
	public static void main(String[] args) throws IOException {
		//Converting
		InputStream ins = new ByteArrayInputStream("HelloWorld".getBytes());
		
		//Reading
		BufferedReader reader = new BufferedReader(new InputStreamReader(ins));
		String line;
		
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
	}
}

Output



HelloWorld


0 comments: