Download webpage in Java



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.
Updated on: 2020-06-21T13:16:19+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements