// CSE 143, Winter 2009, Marty Stepp // This client program uses the Downloader class to download a web page. // It can use either a Downloader or TallyDownloader object. import java.io.*; import java.net.*; import java.util.*; public class DownloadMain { public static void main(String[] args) { // prompt for input URL Scanner console = new Scanner(System.in); System.out.print("URL to download? "); String urlString = console.nextLine(); // create a downloader; re-prompt the user if this fails Downloader down = null; while (down == null) { try { // creating the Downloader can throw a MalformedURLException // down = new Downloader(urlString); down = new TallyDownloader(urlString); } catch (MalformedURLException e) { // run this code if the code throws the exception System.out.print("Bad URL! Try again: "); urlString = console.nextLine(); } } // prompt for output file name System.out.print("Target file name: "); String targetFileName = console.nextLine(); // download bytes to file (print error message if it fails) try { down.download(targetFileName); } catch (IOException e) { System.out.println("An I/O error occurred: " + e.getMessage()); } } }