How to read the contents of a web page without using any external library in Java?

JavaObject Oriented ProgrammingProgramming

The URL class of the package represents a Uniform Resource Locator which is used to point a resource (file or, directory or a reference) in the world wide web.

The openStream() method of this class opens a connection to the URL represented by the current object and returns an InputStream object using which you can read data from the URL.

Therefore, to read data from web page (using the URL class) −

  • Instantiate the class by passing the URL of the desired web page as a parameter to its constructor.

  • Invoke the openStream() method and retrieve the InputStream object.

  • Instantiate the Scanner class by passing the above retrieved InputStream object as a parameter.


import java.util.Scanner;
public class ReadingWebPage {
   public static void main(String args[]) throws IOException {
      //Instantiating the URL class
      URL url = new URL("");
      //Retrieving the contents of the specified page
      Scanner sc = new Scanner(url.openStream());
      //Instantiating the StringBuffer class to hold the result
      StringBuffer sb = new StringBuffer();
      while(sc.hasNext()) {
      //Retrieving the String from the String Buffer object
      String result = sb.toString();
      //Removing the HTML tags
      result = result.replaceAll("<[^>]*>", "");
      System.out.println("Contents of the web page: "+result);


Contents of the web page: Itworks!
Updated on 11-Oct-2019 06:38:41