Read InputStream to String and convert new line to <BR> - Java java.io

Java examples for java.io:InputStream Read

Description

Read InputStream to String and convert new line to <BR>

Demo Code

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

public class Main {

  public static String toConvertString(InputStream is) {
    StringBuilder res = new StringBuilder();
    InputStreamReader isr = new InputStreamReader(is);
    BufferedReader read = new BufferedReader(isr);
    try {//w w w . ja va  2 s .co m
      String line;
      line = read.readLine();
      while (line != null) {
        res.append(line).append("<br>");
        line = read.readLine();
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        isr.close();
        read.close();
        is.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
    return res.toString();
  }

}

Related Tutorials