Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6764439
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T14:35:39+00:00 2026-05-26T14:35:39+00:00

For a program I’m trying to create a directory tree. So the first part

  • 0

For a program I’m trying to create a directory tree. So the first part of my program uses Paths to traverse the directory I need:

public static void main(String[] args) throws IOException {
    Path startingDir = Paths.get("/home/somedirectory");
    PrintFiles pf = new PrintFiles();
    Files.walkFileTree(startingDir, pf);

}

And the PrintFiles program (I directly copied this from the guide on how to use paths for walking a tree) http://download.oracle.com/javase/tutorial/essential/io/walk.html:

public static class PrintFiles extends SimpleFileVisitor<Path> {

    //Print information about each type of file.
    @Override
    public FileVisitResult visitFile(Path file, BasicFileAttributes attr) {
        if (attr.isSymbolicLink()) {
            System.out.format("Symbolic link: %s ", file);
        } else if (attr.isRegularFile()) {
            System.out.format("Regular file: %s ", file);
        } else {
            System.out.format("Other: %s ", file);
        }
        System.out.println("(" + attr.size() + "bytes)");
        return CONTINUE;
    }

 //Print each directory visited.
    @Override
    public FileVisitResult postVisitDirectory(Path dir, IOException exc) {
        System.out.format("Directory: %s%n", dir);
        return CONTINUE;
    }

    public FileVisitResult visitFileFailed(Path file, IOException exc) {
        System.err.println(exc);
        return CONTINUE;
    }

}

I also have a generic tree program that creates a tree with any number of nodes, with the typical add nodes remove nodes etc functions (I’m not going to post the code because its long and I don’t think its really necessary since its a pretty standard implementation).

My question is how exactly do I do something where I can create a generic tree that represents the directory tree for my specific directory? I’m not exactly familiar with how the Path and File libraries work.

Thanks,
Kevin

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T14:35:40+00:00Added an answer on May 26, 2026 at 2:35 pm

    You’ll need to implement preVisitDirectory, postVisitDirectory and visitFile. And you’ll need to hold the “current node” in your visit implementation.

    • In pre create a new node, add it to currentNode and then set currentNode = newNode
    • In visitFile create a new leaf node and add it to currentNode
    • In post set currentNode = currentNode.getParent()

    So sort of like a stack operation where you “push” in preVisit and “pop” in postVisit.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My Program overrides public void paint(Graphics g, int x, int y); in order to
this program hangs after taking first argument:- #include <stdio.h> #include <conio.h> void ellip(char*,...); int
class Program { static void Main(string[] args) { string value = "12345"; Type enumType
The program I'm working on crashes sometimes trying to read data at the address
My program need to occupy some resources. I want it to release those resources
Program : void DibLaplacian8Direct(CDib sourceImg) { register int i,j; int w = sourceImg.GetWidth(); int
This program is really getting on my nerves: I am trying to read a
My program, finds any subwindows, the case if an 3-part app shows an Error
Program followed by output. Someone please explain to me why 10,000,000 milliseconds from Jan
Most program languages have some kind of exception handling; some languages have return codes,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.