beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

How To Convert InputStream To String In Java

By Chaitanya Singh | Filed Under: Java Conversion

Here is the complete example of how to read and convert an InputStream to a String. The steps involved are:
1) I have initialized the InputStream after converting the file content to bytes using getBytes() method and then using the ByteArrayInputStream which contains an internal buffer that contains bytes that may be read from the stream.
2) Read the InputStream using InputStreamReader.
3) Read InputStreamReader using BufferedReader.
4) Appended each line to a StringBuilder object which has been read by BufferedReader.
5) Finally converted the StringBuilder to String using toString() method.

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class Example {
   public static void main(String[] args) throws IOException {
       InputStreamReader isr = null;
       BufferedReader br = null;
       InputStream is = 
            new ByteArrayInputStream("This is the content of my file".getBytes());
       StringBuilder sb = new StringBuilder();
       String content;
       try {
           isr = new InputStreamReader(is);
	   br = new BufferedReader(isr);
	   while ((content = br.readLine()) != null) {
		sb.append(content);
	   }
	} catch (IOException ioe) {
		System.out.println("IO Exception occurred");
		ioe.printStackTrace();	
	   } finally {
		isr.close();
		br.close();
	      }
        String mystring = sb.toString();
	System.out.println(mystring);
   }
}

Output:

This is the content of my file

References:

  • BufferedReader
  • ByteArrayInputStream
  • InputStream
  • InputStreamReader
❮ Previous

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2021 BeginnersBook . Privacy Policy . Sitemap