Link to home
Start Free TrialLog in
Avatar of shannon_cogan
shannon_cogan

asked on

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
Avatar of msmolyak
msmolyak

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.
ASKER CERTIFIED SOLUTION
Avatar of prajod
prajod

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.*;
import java.io.*;

public class printDir extends Object
{



        public void printDir(String dir) {


                getDirectories(dirname);

      }
      

    // 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