Solved

Reading DIR, Printing Contents

Posted on 1998-07-15
3
177 Views
Last Modified: 2010-03-30
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
0
Comment
Question by:shannon_cogan
3 Comments
 
LVL 5

Expert Comment

by:msmolyak
ID: 1226702
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.
0
 

Accepted Solution

by:
prajod earned 50 total points
ID: 1226703
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 java.io.File and java.io.FileNameFilter

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()
  {
    try
    {
      /* 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>");
    }
  }
}

0
 

Expert Comment

by:barrs
ID: 1226704
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


0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look lucid like it was made of glass. Or: how to make your component more Apple-ish ;) This tip assumes your component to be of rectangular shape and completely opaque. (COD…
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
The viewer will learn how to implement Singleton Design Pattern in Java.

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now