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 7877223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:18:22+00:00 2026-06-03T03:18:22+00:00

I need to do something with the file visited last in a directory. How

  • 0

I need to do something with the file visited last in a directory. How can I know if the current call to my visitFile() is the last one?

(I only want to list all the files and directories in a given directory. To do so, I’ve introduced a depth field to my FileVisitor implementation and in the preVisitDirectory I return SKIP_SUBTREE if the depth is greater than 0. (And then increment the depth.) The problem is that I don’t know when to reset the depth to 0, because when I call the walkFileTree with this FileVisitor implementation for another directory, the depth is already > 0 and it only lists the given directory.)

  • 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-06-03T03:18:23+00:00Added an answer on June 3, 2026 at 3:18 am

    How about maintaining the depth only within the two methods, preVisitDirectory and postVisitDirectory? You’ll increment depth in preVisitDirectory and decrement it in postVisitDirectory. You might have to initialize depth to -1, to have depth == 0 when in the start directory though. That way, you’ll always have the right depth.

    Edit: If you return SKIP_SIBLINGS from visitFile, instead of from preVisitDirectory, the postVisitDirectory will still get called!

    Here’s a code sample:

    public class Java7FileVisitorExample {
    
    public void traverseFolder(Path start){
        try {
            Files.walkFileTree(start, new SimpleFileVisitor<Path>() {
    
                private int depth = -1;
    
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs)
                        throws IOException {
                    System.out.println("preVisitDirectory(" + dir + ")");
                    depth++;
                    return FileVisitResult.CONTINUE;
                }
    
                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
                        throws IOException {
                    if (depth > 0) {
                        return FileVisitResult.SKIP_SIBLINGS;
                    }
    
                    System.out.println("visitFile(" + file + ", " + attrs + "): depth == " + depth);
    
                    return FileVisitResult.CONTINUE;
                }
    
                @Override
                public FileVisitResult postVisitDirectory(Path dir, IOException e)
                        throws IOException {
                    if (e == null) {
                        depth--;
                        System.out.println("postVisitDirectory(" + dir + ")");
                        return FileVisitResult.CONTINUE;
                    } else {
                        throw e;
                    }
    
    
                }
            });
        } catch (IOException ex) {
            Logger.getAnonymousLogger().throwing(getClass().getName(), 
                    "traverseFolder", ex);
        }
    }
    
    public static void main(String... args) {
        Path start = Paths.get("/Book/Algorithm");
        new Java7FileVisitorExample().traverseFolder(start);
    }
    

    }

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

Sidebar

Related Questions

I need something that can generate a PDB from a DLL file (C# .NET
I'm creating something that includes a file upload service of sorts, and I need
In C#, Whenever i need to get file path more dyanmically, i do something
I need something like a 3-dimension (like a list or a map), which I
I need something like a temporary in-memory key-value store. I know there are solutions
How do I check if a file is empty in C#? I need something
i want to copy a file (example: a.jpg) to all folders in a directory.
I need to write data to a .txt file in MATLAB. I know how
I need something that takes a directory's contents and then proceeds to generate an
I would like to know who can I retrieve the date from a file.

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.