Scripts: Scripts: Java :: Using java.net tools :: Library Article #3

Developer's Section

Read a Webpage using Java
By: Erobo Team Member

Hire a Developer for Related Work / Installation | $55 hr
Rating: 
Average Votes: (1990)
Rate It:   
Favorites:

Learn to read the contents of a webpage using the java.net tools.

In this article we will be looking at one of the most important java.net components: the URL object. When we instantiate an object of type URL, we can create a stream using the URL.openStream() method. This in turn will allow us to use an InputStreamReader object to read the contents of the script into a Buffer Reader.

Example:

 Code Snippet 1

import java.net.*;
import java.io.*;

public class readSite {

  public static void main(String args[]) {
  
  try
  {
   //read the contents of http://www.internet.com
   //and output them to the console window
    URL siteUrl = new URL("http://www.internet.com");
    BufferedReader in = new BufferedReader(
                                        new InputStreamReader(
                                        siteUrl.openStream()));

    String inputLine;

    while ((inputLine = in.readLine()) != null)
        System.out.println(inputLine);

    in.close();

  }
  catch(MalformedURLException e)
  {
   System.out.println("The Url doesn't exist");
  }
  catch(IOException e)
  {
   System.out.println("an Exception has occured");
  }

  }
  

  }


See other Scripts in Using java.net tools

 

Submit Your Scripts:


System Message:
  • Your Java script has been sent. Please allow 48 hours for processing.


If you would like to have your Java scripts published in this section please fill out
the form below:
*Your Name or
Username:
Home Town:
*Email:
*Description and Code:
*Enter Code shown
to the right:

[ Refresh Image ]
  

 


© 2018 Erobo Software, Inc. All Rights Reserved. | Read Privacy Notice | RSS