// // example of use of java.net.* classes: copy contents of URL to // standard out. // adapted from example in ch. 12 of _Exploring Java_, Niemeyer & Peck. // import java.net.* ; import java.io.* ; // // no constructor or methods; see main method. // class ShowURLContents { // ---- main -------------------------------------------------- // // command-line argument is URL. // program writes contents of URL (assumed to be an ASCII file, // probably HTML) to standard out. // // examples: // java ShowURLContents // http://www.cise.ufl.edu/~blm/cis4930 // java ShowURLContents http://www.ufl.edu // public static void main(String[] args) { if (args.length < 1) { System.out.println("Argument is URL") ; System.exit(-1) ; } // create URL object. URL u = null ; try { u = new URL(args[0]) ; } catch (MalformedURLException e) { System.out.println("Bad URL " + args[0]) ; System.out.println("Exception = " + e) ; System.exit(-1) ; } // open input stream for URL. InputStream in = null ; try { in = u.openStream() ; } catch (IOException e) { System.out.println("Unable to open " + args[0]) ; System.out.println("Exception = " + e) ; System.exit(-1) ; } // read contents and copy to standard output. try { BufferedReader br = new BufferedReader( new InputStreamReader(in)) ; System.out.println("Contents of " + args[0] + ":") ; String line ; while ( (line = br.readLine()) != null) System.out.println(line) ; br.close() ; } catch (IOException e) { System.out.println("I/O error reading " + args[0]) ; System.out.println("Exception = " + e) ; System.exit(-1) ; } } }