How to Convert String to InputStream in Java


In this example, we will learn how to convert String to InputStream in Java.

Source Code

package com.beginner.examples;

import java.io.*;

public class FileOperator25Example { 
	public static void main(String[] args) throws IOException{

		String str = "string";

		InputStream is = new ByteArrayInputStream(str.getBytes());
		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		String line;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
		reader.close();
    }
}

Output:

string
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments