Java Reference
In-Depth Information
Example 3−8: HTMLWriter.java (continued)
* writes them out to an HTMLWriter stream. It will only work in
* Netscape 4.0 or later. It requires an <APPLET> tag like this:
* <APPLET CODE="HTMLWriter$Test.class" WIDTH=10 HEIGHT=10 MAYSCRIPT>
* <PARAM NAME="url" VALUE="HTMLWriter.java">
* </APPLET>
* Note that MAYSCRIPT attribute. It is required to enable the applet
* to invoke JavaScript.
**/
public static class Test extends Applet {
HTMLWriter out;
/** When the applet starts, read and display specified URL */
public void init() {
try {
// Get the URL specified in the <PARAM> tag
URL url =
new URL(this.getDocumentBase(), this.getParameter("url"));
// Get a stream to read its contents
Reader in = new InputStreamReader(url.openStream());
// Create an HTMLWriter stream for out output
out = new HTMLWriter(this, 400, 200);
// Read buffers of characters and output them to the HTMLWriter
char[] buffer = new char[4096];
int numchars;
while((numchars = in.read(buffer)) != -1)
out.write(buffer, 0, numchars);
// Close the streams
in.close();
out.close();
}
catch (IOException e) {}
}
/** When the applet terminates, close the window we created */
public void destroy() { out.closeWindow(); }
}
}
Exercises
3-1. Write a program named Head that prints out the first 10 lines of each file spec-
ified on the command line.
3-2. Write a corresponding program named Tail that prints out the last 10 lines of
each file specified on the command line.
3-3. Write a program that counts and reports the number of lines, words, and
characters in a specified file. Use static methods of the java.lang.Character
class to determine whether a given character is a space (and therefore the
boundary between two words).
3-4. Write a program that adds up and reports the size of all files in a specified
directory. It should recursively scan any subdirectories, summing and report-
ing the size of the files that they contain, and incorporate those directory sizes
into its final output.
Search WWH ::




Custom Search