// CSE 143, Autumn 2013 // 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 target) throws IOException { InputStream in = url.openStream(); FileOutputStream out = new FileOutputStream(target); // read each byte from the URL and write it to the stream int n = in.read(); while(n != -1) { out.write(n); n = in.read(); } in.close(); out.close(); } }