Read a web page character by character in Java

Description

The following code shows how to read a web page character by character.

Example


  //from  www  .j  a va 2  s.  com
import java.io.BufferedInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
import java.net.URLConnection;

public class Main {

  public static void main(String[] args) throws Exception {

    URL u = new URL("http://www.java2s.com");
    URLConnection uc = u.openConnection();
    InputStream raw = uc.getInputStream();
    InputStream buffer = new BufferedInputStream(raw);

    Reader r = new InputStreamReader(buffer);
    int c;
    while ((c = r.read()) != -1) {
      System.out.print((char) c);
    }
  }

}

The code above generates the following result.





















Home »
  Java Tutorial »
    Network »




NetworkInterface
URI
URL
HTTP
HTTP Read
IP
Socket
UDP
URL Encode