Advertisement
cd62131

URL get

Feb 19th, 2014
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.02 KB | None | 0 0
  1. import java.io.FileWriter;
  2. import java.io.Writer;
  3. import java.net.URL;
  4. import java.util.Scanner;
  5.  
  6. public class A {
  7.   public static void main(String[] args) {
  8.     if (args.length < 2) System.exit(1);
  9.     URL url = null;
  10.     try {
  11.       url = new URL(args[0]);
  12.     }
  13.     catch (Exception e) {
  14.       System.err.println("url error.");
  15.       System.exit(1);
  16.     }
  17.     Scanner in = null;
  18.     try {
  19.       in = new Scanner(url.openStream());
  20.     }
  21.     catch (Exception e) {
  22.       System.err.println("url can't open.");
  23.       System.exit(1);
  24.     }
  25.     Writer out = null;
  26.     try {
  27.       out = new FileWriter(args[1]);
  28.     }
  29.     catch (Exception e) {
  30.       System.err.println("write file error.");
  31.       System.exit(1);
  32.     }
  33.     StringBuffer sb = new StringBuffer();
  34.     while (in.hasNextLine()) {
  35.       sb.append(in.nextLine());
  36.       sb.append('\n');
  37.     }
  38.     try {
  39.       out.write(sb.toString());
  40.     }
  41.     catch (Exception e) {
  42.       System.err.println("write error.");
  43.       System.exit(1);
  44.     }
  45.   }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement