Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Reading DIR, Printing Contents

Posted on 1998-07-15
3
Medium Priority
?
185 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 100 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses

636 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