Solved

Reading DIR, Printing Contents

Posted on 1998-07-15
3
179 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
allswap challenge 6 80
Eclipse IDE - Cannot copy/paste from console output 8 132
split string containing \r\n in Java 46 38
type mismatch (Object[] to double[] 4 14
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

863 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

24 Experts available now in Live!

Get 1:1 Help Now