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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:46:08+00:00 2026-05-28T18:46:08+00:00

What happens if the first file this code encounters is a dir. Why does

  • 0

What happens if the first file this code encounters is a dir. Why does it not ignore all other files in that dir (therefore obscuring the results)?

This is taken from How do I iterate through the files in a directory in Java?

I’m not trying to dispute that this code works but how does it account for above scenario ?

 public static void main(String... args) {
        File[] files = new File("C:/").listFiles();
        showFiles(files);
    }

    public static void showFiles(File[] files) {
        for (File file : files) {
            if (file.isDirectory()) {
                System.out.println("Directory: " + file.getName());
                showFiles(file.listFiles()); // Calls same method again.
            } else {
                System.out.println("File: " + file.getName());
            }
        }
    }
  • 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-28T18:46:09+00:00Added an answer on May 28, 2026 at 6:46 pm

    It doesn’t ignore it because it’s just making another recursive call – the original call (with the collection of files from the top level) is still on the stack. It makes a new call with a new list of files, referenced by a new parameter called files in a new stack frame.

    So if there’s a directory structure of c:/a/b/c you’ll end up with a stack of:

    showFiles([c:/a/b/c])
    showFiles([c:/a/b])
    showFiles([c:/a])
    main(...)
    

    When the “deepest” call (the top of the stack) returns, the next stack frame will still know about any other files at the same level, and may well recurse again.

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

Sidebar

Related Questions

This happens repeatedly and is very annoying. I upload some PHP code to a
The thing is like this. I have a zip file with images that uncompressing
can you help me improve my code... this is all about student information... i'm
Well I know that selenium world is full of file upload threads, and this
My Cocoa application displays file icons for all files in a given folder. I'm
I'll go first. I'm 100% in the set-operations camp. But what happens when the
I have two pages, on the first page after an event happens I change
Problem on WindowsXP (likely will happen on all Win installs), first time using Mercurial.
What happens to the name/value pairs stored inside a form's resx file? Are they
I'm using the code from this link to send attachments in the email notifications

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.