Solved

Reading DIR, Printing Contents

Posted on 1998-07-15
3
183 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

728 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