O'Reilly logo

Java Cookbook by Ian F. Darwin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Program: MkIndex

This little program has saved me a great deal of time over the years. It reads a directory containing a large number of files, harking back from a time when I kept all my demonstration Java programs in a fairly flat directory structure. MkIndex, shown in Example 17-7, produces a better-formatted listing than the default directory that web servers generate. For one thing, it includes an alphabet navigator, which lets you jump directly to the section of files whose names begin with a certain letter, saving a lot of scrolling time or iterations with the browser’s find menu. This program uses a File object (see Section 10.2) to list the files, and another to decide which are files and which are directories. It also uses Collections.sort (see Section 7.9) to sort the names alphabetically before generating the output. It writes its output to the file index.html in the current directory, even if an alternate directory argument is given. This is the default filename for most standard web servers; if your web server uses something different, of course, you can rename the file.

Example 17-7. MkIndex.java

/** MkIndex -- make a static index.html for a Java Source directory */ public class MkIndex { /** The output file that we create */ public static final String OUTPUTFILE = "index-byname.html"; /** The string for TITLE and H1 */ public static final String TITLE = "Ian Darwin's Java Cookbook: Source Code: By Name"; /** The main output stream */ PrintWriter out; /** The ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required