The URL class:
The class URL has a number of constructors that allow the programmer to create uniform resource locators. For the moment, we consider only the simplest constructor - a single argument constructor that creates a URL object from its string description. For example:
URL openUniversity = new URL("https://www.open.ac.uk");
An important method, called openStream, opens a stream to a web resource such as a web page and allows the programmer to access the contents of the resource.
The following code is for a class called WebReader that accesses a website and displays the contents of the home page of the site. If you execute the associated test program, you will see the HTML for the home page of The Open University displayed on System.out.
import java.net. *;
import java.io. *;
public class WebReader
{
private BufferedReader fromWebSite;
private String webAddress;
public WebReader (String address)
{
webAddress = address;
try
{
System.out.println("Trying to contact " + webAddress);
URL selectedURL = new URL(webAddress);
fromWebSite = new BufferedReader(
new InputStreamReader(
selectedURL.openStream( )));
}
catch (MalformedURLException me)
{
System.out.println("Malformed URL found " + me);
}
catch (IOException io)
{
System.out.println("Problems connecting " + io);
}
}
/ * Read and display source contents of web page. Uses BufferedReader fromWebSite, set up by constructor. */
public void print ( )
{
String lineRead;
System.out.println(" - - - URL: " + webAddress);
try
{
// read from website, one line at a time
lineRead = fromWebSite.readLine( );
while (lineRead ! = null)
{
System.out.println(lineRead);
lineRead = fromWebSite.readLine( );
}
fromWebSite.close( );
}
catch (IOException io)
{
System.out.println("Problems reading " + io );
}
System.out.println(" - - - End of URL: " + webAddress);
}
}
The class WebReader has a constructor and a public method print. The constructor instantiates a URL object for the address supplied as a String argument. It then accesses the input stream for this URL object and uses this to create a BufferedReader for inputting character data from the resource.
Two possible exceptions could be generated in the constructor. A MalformedURLException could occur when the URL object is constructed and an IOException could arise when the stream is associated with the web resource. These are handled by separate catch clauses - it is important that the MalformedURLException clause occurs first.
The print method reads from the resource (in this case, a web page) one line at a time and displays each line on System.out. The method terminates when there are no more lines to read, indicated by the readLine method of the buffered reader returning a null reference. Before the method terminates, it closes the stream fromWebSite.
The following class shows how we use a WebReader object:
public class TestWebReader
{
public static void main (String [] args)
{
WebReader wr = new WebReader("https://www.open.ac.uk");
wr.print( );
}
}
The main method creates a WebReader object for the Open University home page and invokes the print method to display the source (that is, the HTML) for this page. You can compare this output with the result you obtain when you visit this site using a browser - select a browser menu option to display the web page source.
Java Assignment Help - Java Homework Help
Struggling with java programming language? Are you not finding solution for your The URL class homework and assignments? Live The URL class experts are working for students by solving their doubts & questions during their course studies and training program. We at Expertsmind.com offer The URL class homework help, java assignment help and The URL class projects help anytime from anywhere for 24x7 hours. Computer science programming assignments help making life easy for students.
Why Expertsmind for assignment help
- Higher degree holder and experienced experts network
- Punctuality and responsibility of work
- Quality solution with 100% plagiarism free answers
- Time on Delivery
- Privacy of information and details
- Excellence in solving java programming language queries in excels and word format.
- Best tutoring assistance 24x7 hours