Java Reference
In-Depth Information
The above value will be copied into the line variable that is then parsed.
if (line != null)
{
int cityNum = Integer.parseInt(extract(line, "=", "\"", 2));
int i = line.indexOf(">");
String cityName = line.substring(i + 1);
Date cityTime = getCityTime(cityNum);
Next, we will parse out the city number by extracting what is between the = and the
quote character. Given the line extracted (shown above), the extract function should return a
“2” for Baltimore. Finally, we parse the city and state by searching for a > symbol. Extracting
everything to the right of the > symbol will give us “Baltimore, MD”. We now have the city's
number, as well as its name and state.
We now can pass the city's number into the getCityTime function. The
getCityTime function performs the same operation as the last recipe; that is, it will ac-
cess the URL for the city for which we are seeking the time. The time will be returned as a
string. For more information about how the getCityTime function works, review Recipe
3.2.
Now that we have the city time, we will format it using SimpleDateFormat as
shown below:
SimpleDateFormat sdf = new SimpleDateFormat("hh:mm:ss aa");
String time = sdf.format(cityTime);
System.out.println(count + " " + cityName + "\t" + time);
} else
done = true;
count++;
}
You may notice in the above code, that in this program, the time is formatted to exclude
the date. This allows us to display each of the cities, and what the current time is, without
displaying the date.
This recipe can be revised and applied to any real-world site that contains a list that leads
to multiple other pages that you wish to extract data from.
Recipe #3.4: Downloading a Binary File
The last two recipes for this chapter will demonstrate how to download data from a web
site directly to a disk file. The first recipe will download to a binary file while the second will
show how to download to a text file. A binary file download will make an exact copy of what
was at the URL. The binary download is best used with a non-text resource, such as an image,
sound or application file. Text files must be treated differently and will be discussed in detail
in recipe 3.5.
Search WWH ::




Custom Search