Java Data Type How to - Convert the DataInputStream to the String








Question

We would like to know how to convert the DataInputStream to the String.

Answer

import java.io.DataInputStream;
import java.net.URL;
import java.net.URLConnection;
//from ww w  .j  a v  a  2s  . co  m
public class Main {
  public static void main(String[] args) throws Exception {

    URL google = new URL("http://www.google.com/");
    URLConnection googleConnection = google.openConnection();
    DataInputStream dis = new DataInputStream(googleConnection.getInputStream());
    StringBuffer inputLine = new StringBuffer();
    String tmp;
    while ((tmp = dis.readLine()) != null) {
      inputLine.append(tmp);
      System.out.println(tmp);
    }
    // use inputLine.toString(); here it would have whole source
    dis.close();
  }
}