How to get any webpage/URL content and save it on local drive using java code.

Alright, so today we are going to be coding to get the snapshot of any website. Here, we are using two jdk inbuilt packages java.io and java.net
This code is pretty self explanatory however, wherever required I've added the comments.
package com.amzi;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class GetWebpageContent {
 public static void main(String[] args) {

  URL url;

  try {
   // enter any url to get its content
   url = new URL("http://www.javaandj2eetutor.blogspot.com");
   URLConnection conn = url.openConnection();

   // open the stream and put it into BufferedReader
   BufferedReader br = new BufferedReader(new InputStreamReader(
     conn.getInputStream()));
   String inputLine;

   // save it anywhere in local machine for offline use
   String fileName = "/users/Amzi/test.html";
   File file = new File(fileName);
   if (!file.exists()) {
    file.createNewFile();
   }

   FileWriter fw = new FileWriter(file.getAbsoluteFile());
   BufferedWriter bw = new BufferedWriter(fw);

   while ((inputLine = br.readLine()) != null) {
    bw.write(inputLine);
   }

   bw.close();
   br.close();

   System.out.println("Your file is saved in " + fileName
     + " location.");

  } catch (MalformedURLException e) {
   e.printStackTrace();
  } catch (IOException e) {
   e.printStackTrace();
  }

 }
}

Once we run the above program, if everything goes fine then the URL content supposed to be saved in the location we specified in the program and in the console weare supposed to see something like this.
Your file is saved in /users/Amzi/test.html location.

4 comments:

  1. how to convert it as a desktop application. i need desktop application for this program.

    ReplyDelete
  2. Thank you for sharing such an informative article. I really hope I can see other interesting posts. Keep up the good work!


    Mobile App developer

    ReplyDelete
  3. I read this article. I think You put a lot of effort to create this article. I appreciate your work.
    thesis Writing Service

    ReplyDelete
  4. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here.
    Kindly keep blogging. If anyone wants to become a Java developer learn from Java EE Online Training from India.
    or learn thru Java EE Online Training from India . Nowadays Java has tons of job opportunities on various vertical industry.

    ReplyDelete