How to Convert InputStream to String in Java


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

Source Code

package com.beginner.examples;

import java.io.*;

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

		InputStream in = new ByteArrayInputStream("inputStream".getBytes());
		BufferedReader br = null;
		String content= "";

		String line;
		try 
		{
			br = new BufferedReader(new InputStreamReader(in));
			while ((line = br.readLine()) != null)
			{
				content += line;
			}

		} 
		catch (IOException e) 
		{
			e.printStackTrace();
		}
		System.out.println(content);
    }
}

Output:

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