728x90 AdSpace

Tuesday, 1 October 2013

Convert InputStream to String using Java IO

In this tutorials we are going to see how to convert InputStream to String using methods available in Java IO.
  1. Create project called JavaMisc.
  2. Create package called com.javatutorialscorner.io
  3. Create java class called InputStreamToString under com.javatutorialscorner.io package.
package com.javatutorialscorner.io;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class InputStreamToString {

* @param args
public static void main(String[] args) {
InputStream is = null;

try {
is = new FileInputStream(new File(
String contentInFile = toString(is);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block


private static String toString(InputStream is) {
BufferedReader br = null;
StringBuilder sb = null;
String templine = "";

try {
sb = new StringBuilder();

br = new BufferedReader(new InputStreamReader(is));
while ((templine = br.readLine()) != null) {
} catch (IOException e) {
} finally {
if (br != null) {
try {
} catch (IOException e) {

return sb.toString();

Now run the program see the following output in console.

this is test file created by java tutorials corner

This content read from input file
Annamalai Thangaraj

Annamalai is working as Technical Lead in Leading Telecom company with 5+ years experience in Identity and Access Management , Telecom and Networks, BigData, Java, Spring, Struts, Hibernate, AngularJS, and Enterprise Web Application Development.

Website: Java Tutorials Corner

Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments


Post a Comment

Item Reviewed: Convert InputStream to String using Java IO Rating: 5 Reviewed By: Annamalai Thangaraj