Example of Dir Stack

From Java Example Source Code

Jump to: navigation, search

Contents

[edit] Overview - Example of Dir Stack

This is a example of Java program.

[edit] Java Source Code

  • Package: Horstmann.Cay
  • File: DirStack.java
package Horstmann.Cay;
 
/**
 * @version 1.10 1999-07-07
 * @author Cay Horstmann
 */
 
import java.io.File;
import java.io.IOException;
import java.util.Stack;
 
public class DirStack {
    public static void main(String[] args) {
	Stack directoryStack = new Stack();
	File root = new File(File.separator + ".");
	directoryStack.push(root);
	while (directoryStack.size() > 0) {
	    File currentDirectory = (File) directoryStack.pop();
	    System.out.println(currentDirectory);
	    String[] subdirectories = currentDirectory.list();
	    if (subdirectories != null) {
		for (int i = 0; i < subdirectories.length; i++) {
		    try {
			String fname = currentDirectory.getCanonicalPath()
				+ File.separator + subdirectories[i];
			File f = new File(fname);
 
			if (f.isDirectory())
			    directoryStack.push(f);
		    } catch (IOException e) {
			System.out.println(e);
		    }
		}
	    }
	}
    }
}

[edit] What Result You Can Get

Run the program, you will get:


F:\eshow
F:\eshow\lib
F:\eshow\img
F:\eshow\eclipse
F:\eshow\eclipse\configuration
F:\eshow\eclipse\configuration\.settings
F:\eshow\eclipse\configuration\org.eclipse.core.runtime
F:\eshow\eclipse\configuration\org.eclipse.core.runtime\.manager
F:\eshow\eclipse\configuration\org.eclipse.equinox.app
...

[edit] Required External Library for this Java Example

Need nothing.


[edit] How to Run this Java Example Program

We recommend running this Java example program with Eclipse.

For assistance in working with Eclipse, please see How to Run Java Program with Eclipse.

It's fairly easy.

Personal tools