// Allison Obourn // CSE 143, Autumn 2015 // A Downloader object stores a URL and can download a web page // from that URL to save it to a given file on the local hard disk. import java.io.*; import java.net.*; public class Downloader { private URL url; // Constructs a new Downloader to download from the given web site URL. // Precondition: urlString != null public Downloader(String url) throws MalformedURLException { this.url = new URL(url); } // Saves the web page from this Downloader's URL to the given file name. // Throws an IOException if the web page does not exist or if anything // goes wrong when trying to save the file. public void download(String fileName) throws IOException { InputStream in = url.openStream(); OutputStream out = new FileOutputStream(fileName); int success = in.read(); while(success != -1) { out.write(success); success = in.read(); } in.close(); out.close(); } }