Site hosted by Angelfire.com: Build your free website today!

My Home Page

Java: Language of the future

Java Links

The JAVA Homepage
Lots of free java programs

Hello Suheime!!!

Java - Introduction to Programming

Supplementary Chapter S8 Java and the Internet

S8.1 Overview

Java started up in the Internet era. In the Java API, there is a package by the name of "java.net". This package contains classes that handle networking issues. There is also a set of classes that handle internet URLs (Universal Resource Locators).

S8.2 Objective

In this chapter, we will learn how to obtain a BufferedReader for a given URL. In the previous chapter, we had learnt how to read lines off a BufferedReader as well as writing to a file. We will combine these in a program that captures a file off the internet and store it.

S8.3 Saving a URL

Here is the program to save a URL to a file.
import java.io.*;
import java.net.*;

public class S8_ExampleUrl {
	public static void main(String []args) {
		if (args.length<2) {
			System.out.println("Usage: java S8_ExampleUrl <URL> <output file name>");
			System.exit(1);
		}
		S8_ExampleUrl s8=new S8_ExampleUrl(args[0], args[1]);
		s8.readFile();
	}
	protected String urlString="", filename="";
	public S8_ExampleUrl(String url, String file) {
		urlString = url;
		filename = file;
	}
	public void readFile() {
		try {
			URL url = new URL(urlString);
			URLConnection urlCon = url.openConnection();
			PrintWriter pw=new PrintWriter(new FileWriter(filename));
			BufferedReader br=new BufferedReader(new InputStreamReader(urlCon.getInputStream()));
			String input="";
			while ((input=br.readLine()) != null) {
				pw.println(input);
				processLine(input);
			}
			pw.close();
			br.close();
		} catch (Exception e) {e.printStackTrace();}
		System.out.println("URL: " + urlString + " read");
	}
	public void processLine(String input) {
	}
}

S8.4 Exercise

You can find this program at K:\JIP\example\S8_ExampleUrl.java. Add this class to your BlueJ project, then compile and run "void main()". You need to pass the URL that you want to retrieve and the name of the file to store the output. For example, you could use: {"http://www.yahoo.com", "c:/temp/tt.htm"}. After a while, the terminal window would appear telling you that the file has been read.

Navigate to the output file in the Windows Explorer. Double click on it, and you should see the file opened in the Internet Explorer. Note that some resources like images may not be found.

Next, open the same output file in Notepad. Search for this phrase "a href" - these are the hyper-links embedded in the HTML file.
  1. There is a method with the name "processLine(String input)". Add Java statements look for "a href" - you can use the method indexOf() of the String class to find out if "a href" is found. (Note: if the return value is less than 0, it is not found.) Print out those lines that contain "a href"
  2. Change the program so that it will pick up lines that contain "img src" instead.
  3. Try to modify the program so that it shows only the image file name instead of the whole line. You will need to find the start and end locations of the URL, and then use the method substring().
As an option, you could
  1. spend time to try to complete assignments that you have not completed
  2. trace through the assignments you have completed to understand how they work
  3. modify programs, and see if it works as you expect
Thank you for visiting my page at Angelfire. Please come back and visit again! Learn Java: the general programming language that is platform independent. It runs in Web browsers as well.