/** * This simple program uses the URL class and its openStream() method to * download the contents of a URL and copy them to a file or to the console. **/ /* Try this program with two arguments: java GetURL "http://www.sun.com" NewFile */ import java.io.*; import java.net.*; public class GetURL { public static void main(String[] args) { InputStream in = null; OutputStream out = null; try { // Check the arguments if ((args.length != 1)&& (args.length != 2)) throw new IllegalArgumentException("Wrong number of args"); // Set up the streams URL url = new URL(args[0]); // Create the URL // // openStream() method opens incoming InputStream from the url object in = url.openStream(); if (args.length == 2) // Get an appropriate output stream out = new FileOutputStream(args[1]); else out = System.out; // Now copy bytes from the URL to the output stream byte[] buffer = new byte[4096]; int bytes_read; while((bytes_read = in.read(buffer)) != -1) out.write(buffer, 0, bytes_read); } // On exceptions, print error message and usage message. catch (Exception e) { System.err.println(e); System.err.println("Usage: java GetURL []"); } finally { // Always close the streams, no matter what. try { in.close(); out.close(); } catch (Exception e) {} } } }