Java Reference
In-Depth Information
Extracting a Filename
The extractFile function is used to get the filename portion of a URL. Consider
the following URL:
http://www.heatonresearch.com/images/logo.gif
The filename portion is logo.gif . To extract this part of the URL, the path of the
URL is first converted to a string.
String str = u.getFile();
This string is then searched for the last slash (/) character. Everything to the right of the
slash is treated as the filename.
// strip off path information
int i = str.lastIndexOf('/');
if (i != -1)
str = str.substring(i + 1);
return str;
This method is used to strip the filename from each image, so that the image can be
saved locally to this filename.
Recipe #6.6: Extracting from Sub-Pages
So far all of the data that we extracted has been on a single HTML page. Often you will
want to aggregate data spread across many pages. The last two recipes in this chapter show
you how to do this. This recipe shows you how to download data from a list of linked pages.
The list is contained here:
http://www.httprecipes.com/1/6/subpage.php
You can see this list in Figure 6.6.
Search WWH ::




Custom Search