WebPageReader.java.html

  1  import java.io.IOException;
  2  import java.net.URL;
  3  import java.util.Scanner;
  4  
  5  /**
  6     This program prints all lines from a web page that contain
  7     references to other web sites.
  8  */
  9  public class WebPageReader
 10  {
 11     public static void main(String[] args) throws IOException
 12     {
 13        String address = "http://horstmann.com/index.html";
 14        URL pageLocation = new URL(address);
 15        Scanner in = new Scanner(pageLocation.openStream());
 16        while (in.hasNext())
 17        {
 18           String line = in.next();
 19           if (line.contains("href=\"http://")) 
 20           {
 21              int from = line.indexOf("\"");
 22              int to = line.lastIndexOf("\"");
 23              System.out.println(line.substring(from + 1, to));
 24           }
 25        }
 26     }
 27  }