Read input Stream To String via BufferedReader - Java File Path IO

Java examples for File Path IO:BufferedReader

Description

Read input Stream To String via BufferedReader

Demo Code


import org.apache.log4j.Logger;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Collection;

public class Main{
    private final static Logger log = Logger.getLogger(StringUtil.class);
    public static String inputStreamToString(final InputStream inputStream) {

        final BufferedReader br = new BufferedReader(new InputStreamReader(
                inputStream));/*from  ww  w.j  av  a2  s .c  om*/
        final StringBuilder sb = new StringBuilder();

        try {
            while (br.ready()) {
                sb.append(br.readLine());
            }
        } catch (IOException e) {
            log.error("can't read line from buffered reader", e);
        } finally {
            try {
                br.close();
            } catch (IOException e) {
                log.error("can't close buffered reader", e);
            }
        }

        return sb.toString();
    }
}

Related Tutorials