How to convert Java String to InputStream ?

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


No comments:

Post a Comment

Model Context Protocol (MCP) — Complete Guide for Backend Engineers

  Model Context Protocol (MCP) — Complete Guide for Backend Engineers Build Tools, Resources, and AI-Driven Services Using LangChain Moder...

Featured Posts