Monday, 22 December 2014

Program to download a web-page

To Save a web-page into new File and print html of same web page using Java Program

Ques.Write a java Program to read an html of any web-page and print its html code and save that web-page into new html file?

Program:


/**
 * @(#)SWP.java
 *
 *
 * @author Suyash Bhalla
 * @version 1.00 2014/9/24
 */

import java.net.*;
import java.io.*;


public class SWP {
    public static void main(String ar[])throws Exception{
    URL url=new URL("http://www.google.co.in");             //URL of web-page
    URLConnection urlc=url.openConnection();
    BufferedInputStream buffer=new BufferedInputStream(urlc.getInputStream());
    StringBuilder builder=new StringBuilder();
    int html;
    File f=new File("SavedPage.html");
    System.out.println(f.createNewFile());                            //Print true if new file is created.
    FileWriter pen=new FileWriter(f);
    while((html=buffer.read())!=-1){
    pen.write((char)html);
    builder.append((char)html);
    }
    buffer.close();
    pen.close();
    System.out.println(builder);                                           //To Print an html code of Page
    System.out.println("Size of Web Page is:"+(builder.length()/1024)+"MB");
    }   
}

--------X--------X--------X--------X--------X--------X--------X--------X--------X-------X-------

Output:
true
<!doctype html><html itemscope="" itemtype="http://schema.org/WebPage" lang="en-IN"><head><meta content="/images/google_favicon_128.png" itemprop="image"><title............................................................................................................................................................................................................................................................................................................................


</script></div></body></html>
Size of Web Page is:18MB


0 Comments:

Post a Comment

Subscribe to Post Comments [Atom]

<< Home