1.2 Input Streams from URLs

The URL class offers the following method for establishing a connection to a URL and setting an input stream from that connection.

<..URLdata.java..>
 import java.io.*;
 import java.net.*;
 import java.util.*;
 
 public class URLdata {
    public static void main(String[] args) {
       try {
          URL url = new URL( args[0] );
          InputStream is = url.openStream();
          InputStreamReader isr = new InputStreamReader( is );
          BufferedReader in = new BufferedReader( isr );
          System.out.println( "----------------------------------------" );
          System.out.println( args[0] );
          System.out.println( "----------------------------------------" );
          String line;
          for( int i=0; i<15; i++ ){
             if( (line = in.readLine()) == null){ break; }
             System.out.println ( line );
       }  }
       catch (Exception e) {
          System.out.println( "error" );
       }
    }
 }
-_-_-
----------------------------------------  
http://www.cis.ohio-state.edu/  
----------------------------------------  
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/1999/REC-html401-19991224/loose.dtd">  
 
<html>  
 
<head>  
 
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">  
 
<title>The Ohio State University Computer and Information Science Department</title>  
 
<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">  
 
<!--  
 
/* Functions that swaps images. */