// File: IO26.java (Module 13) // // Author: Rahul Simha // Created: Nov 30, 1998 // // Using the URL class. import java.net.*; import java.io.*; public class IO16 { public static void main (String[] argv) { try { // Create a URL instance. URL u = new URL ("http://www.yahoo.com/index.html"); System.out.println ("Protocol: " + u.getProtocol()); System.out.println ("Host: " + u.getHost()); System.out.println ("Port: " + u.getPort()); // Note: default port number is -1, but the protocol handler // knows what to do. System.out.println ("File: " + u.getFile()); // The openStream method makes the connection. InputStream inStream = u.openStream(); // Wrap high-level streams around this. InputStreamReader isr = new InputStreamReader (inStream); LineNumberReader lnr = new LineNumberReader (isr); System.out.println ("Input stream opened"); // Now read the result. String s = lnr.readLine (); while (s != null) { System.out.println (s); s = lnr.readLine (); } lnr.close (); } catch (IOException e) { System.out.println (e); } } }