Convert InputStream to String - Java File Path IO

Java examples for File Path IO:File Stream

Description

Convert InputStream to String

Demo Code


import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Scanner;
 
public class Main {
       //from   w w  w  .  j av a  2 s .c o m
        public static void main(String args[]) throws IOException{
                InputStream is = new FileInputStream("c:/data.txt");
                String strContent;
               
                //Create BufferedReader object
                BufferedReader bReader = new BufferedReader(new InputStreamReader(is));
                StringBuffer sbfFileContents = new StringBuffer();
                String line = null;
               
                //read file line by line
                while( (line = bReader.readLine()) != null){
                        sbfFileContents.append(line);
                }
               
                //finally convert StringBuffer object to String!
                strContent = sbfFileContents.toString();
        }
}

Related Tutorials