Reading DIR, Printing Contents

Greetings JavaLings,

        How do you make a very simple Table of Contents Generator
        that runs in a DOS Window.
        I would like to read the current directory for *.htm, *.html files and
        simply create the links in a index file. (.htm)

        This question is for makeing a Java Application using J++.
         (Does not compile with the latest JDK, good for JDK 1.02 I think.)

        Please, make sure you have tested your code and that it works.

        Thank you,
        Shannon H. Cogan
Who is Participating?
prajodConnect With a Mentor Commented:
Here is a rudimentary class that solves your problem. It generates a table of contents for the current directory only. A more complex and useful class would use the list method of and

Usage example: java Cg *.htm

Note : You can replace FileWriter and PrintWriter with FileStream and PrintStream, if VJ++ gives you errors :-)

public class Cg
  private FileWriter _contentsFile;
  private PrintWriter contentsFile;
  public Cg()
      /* create a contents file, overwrite if it exists */
      _contentsFile = new FileWriter("contents.htm",false);
      contentsFile = new PrintWriter(_contentsFile,true);
    catch (IOException e)
      System.out.println("Can't create contents.htm");
  public static void main(String args[])

    Cg mycg = new Cg();

    for (int i = 0; i< args.length; i++)
    {  System.out.println("File " + i + " = " + args[i]);

       mycg.contentsFile.println("File " + i+1 +": " + " <a href=" + args[i] + ">" +
                args[i] + "</a><br>");

A hint:

if you run your program as
java MyProgram *.htm *.html
the args array in the main() method gets populated with the names of the .htm and .html files in the current directory. AT least it is true for JDK' JVM.
The code below will recursively print out the directory names and
 file names within those directories.

 i.e. It will print out all levels of sub-directory.

import java.lang.Object;
import java.util.*;

public class printDir extends Object

        public void printDir(String dir) {



    // method to recursively read directories and the files contained
    // within.

    public String getDirectories(String dirname)
            File Fdir = new File(dirname);
            String s[] = Fdir.list();
            for (int i = 0; i < s.length; i ++) {
                  String filen = s[i];
                  String name = s[i];
                  File f = new File(dirname + "/" + filen);

                  // Build up the web List to be returned to the client
                  if (f.isDirectory()) {
                                System.out.println("Directory :" + name);
                                page += getDirectort(dirname + "/" + name, false);
                  } else {
                                System.out.println("File :" + name);


} // End of Class

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.