How to read web pages using Java?



In earlier posts we have seen how to read text files and binary files in Java. But how to read a remote resource like a web page in Java. Let’s see how to do it.

Reading a web page

Reading a remote web page is quite similar to the way we have read a text file or a binary file. The IO API’s depict the powerful Decorator pattern is.

Steps:

  • Create an instance of URL
  • Open the connection
  • Get connection input stream
  • Create a BufferedReader and read the content
  • Close the resources

This sounds quite simple. Let’s see the code

The details are quite simple. The BufferedReader part is common to other reading programs.

One application of this simple code code could be fetch the page as part of a big crawler program.

Leave a Reply

Your email address will not be published. Required fields are marked *