Wget.java


Below is the syntax highlighted version of Wget.java from §3.1 Using Data Types.


/******************************************************************************
 *  Compilation:  javac-introcs Wget.java
 *  Execution:    java-introcs Wget url
 *
 *  Reads in a URL specified on the command line and saves its contents
 *  in a file with the given name.
 *
 *  % java-introcs Wget https://introcs.cs.princeton.edu/java/data/codes.csv
 *
 ******************************************************************************/

public class Wget {

    public static void main(String[] args) {

        // read in data from URL
        String url = args[0];
        In in = new In(url);
        String data = in.readAll();

        // write data to a file
        String filename = url.substring(url.lastIndexOf('/') + 1);
        Out out = new Out(filename);
        out.println(data);
        out.close();
    }

}


Copyright © 2000–2017, Robert Sedgewick and Kevin Wayne.
Last updated: Fri May 10 11:00:41 EDT 2019.