Download webpage in Java

Java Programming Java8Object Oriented Programming

We can download a web page using its URL in Java. Following are the steps needed.

  • Create URL object using url string.

    Download webpage in Java
  • Create a BufferReader object using url.openStream() method.

    BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
  • Create a BufferWriter object to write to a file.

    BufferedWriter writer = new BufferedWriter(new FileWriter("page.html"));
  • Read each line using BufferReader and write using BufferWriter.

    String line;
    while ((line = reader.readLine()) != null) {
    writer.write(line);
    }

Following is the complete program to download a given URL page at current location.

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

public class Tester {
   public static void main(String args[]) throws IOException {
      download("http://www.google.com");
   }
   public static void download(String urlString) throws IOException {
      URL url = new URL(urlString);
      try(
         BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
         BufferedWriter writer = new BufferedWriter(new FileWriter("page.html"));
      ) {
         String line;
         while ((line = reader.readLine()) != null) {
            writer.write(line);
         }
         System.out.println("Page downloaded.");
      }
   }
}

Output

Page downloaded.
raja
Published on 13-Aug-2018 16:12:34
Advertisements