import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.FileWriter; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; /** * Program to save web page in a file * * @author sahir maredia (Kotia Solutions) * */ public class SaveWebPage { /** * @param args */ public static void main(String[] args) { if (args.length != 2) { System.out.println("Please provide valid arguments"); return; } try { URL url = new URL(args[0]); BufferedReader br = new BufferedReader(new InputStreamReader( url.openStream())); BufferedWriter bw = new BufferedWriter(new FileWriter(args[1])); String input = ""; while ((input = br.readLine()) != null) { bw.write(input); bw.newLine(); } bw.close(); br.close(); System.out.println("Web page written successfully."); } catch (MalformedURLException e) { System.out.println("Error occured : " + e.getMessage()); } catch (IOException e) { System.out.println("Error occured : " + e.getMessage()); } } }
Run this program as follows :
sahir@sahir-laptop:~/networklab$ javac SaveWebPage.java
sahir@sahir-laptop:~/networklab$ java SaveWebPage http://www.kotiasolutions.com/services.html services.txt
Web page written successfully.
sahir@sahir-laptop:~/networklab$
sahir@sahir-laptop:~/networklab$ java SaveWebPage http://www.kotiasolutions.com/services.html services.txt
Web page written successfully.
sahir@sahir-laptop:~/networklab$