Read InputStream line by line by converting InputStream to BufferedReader - Android java.io

Android examples for java.io:InputStream

Description

Read InputStream line by line by converting InputStream to BufferedReader

Demo Code

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

public class Main {

  public static String getStringByStream(InputStream is) {
    try {//from  w w  w.  java 2 s .  com
      BufferedReader in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
      StringBuffer buffer = new StringBuffer();
      String line = "";
      while ((line = in.readLine()) != null) {
        buffer.append(line + "\n");
      }
      return buffer.toString().replaceAll("\n\n", "\n");
    } catch (Exception e) {
    }
    return null;
  }

}

Related Tutorials