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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:08:42+00:00 2026-05-15T09:08:42+00:00

The following codes goes through all directories and sub-directories and outputs just .java files;

  • 0

The following codes goes through all directories and sub-directories and outputs just .java files;

import java.io.File;


public class DirectoryReader {
    private static String extension = "none";
    private static String fileName;

    public static void main(String[] args ){
        String dir = "C:/tmp";
        File aFile = new File(dir);
        ReadDirectory(aFile);
    }
    private static void ReadDirectory(File aFile) {
        File[] listOfFiles = aFile.listFiles();
        if (aFile.isDirectory()) {
             listOfFiles = aFile.listFiles();
            if(listOfFiles!=null) {
                for(int i=0; i < listOfFiles.length; i++  ) {
                    if (listOfFiles[i].isFile()) {

                        fileName = listOfFiles[i].toString();
                        int dotPos = fileName.lastIndexOf(".");
                        if (dotPos > 0) {
                            extension = fileName.substring(dotPos);
                        }                       
                        if (extension.equals(".java")) {
                        System.out.println("FILE:" + listOfFiles[i] );
                        }
                    }           
                    if(listOfFiles[i].isDirectory()) {
                        ReadDirectory(listOfFiles[i]);
                    }
                }
            }
        }
    }


}

Is this efficient? What could be done to increase the speed?

All ideas are welcome.

  • 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-15T09:08:43+00:00Added an answer on May 15, 2026 at 9:08 am

    My comments:

    • Possible bug: extension is not reset. Think about what happens if you ecounter file1.java and thes file2 (without a .-character?)

      Suggestion: put

      if (extension.equals(".java"))
          System.out.println("FILE:" + listOfFiles[i] );
      

      inside the body of the if (dotPos > 0)

    • ReadDirectory is a method and should accourding to convention have small first letter: readDirectory

    • You could clean up the code a bit by using the for-each loop:

      for(File f : listOfFiles) {
          if (f.isFile()) {
              // ...
          }           
          if(f.isDirectory()) {
              readDirectory(f);
          }
      }
      
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After getting the objectEnumerator in the following code, the set1 retain count goes to
The following code is in the /Courses/Detail action: [AcceptVerbs(GET)] public ActionResult Detail(int id) {
I have the following code: public void DriveRecursion(string retPath) { string pattern = @[~#&!%\+\{\}]+;
Been following the Core Data tutorial on Apple's developer site , and all is
Following code, when compiled and run with g++, prints '1' twice, whereas I expect
The following code works great in IE, but not in FF or Safari. I
The following code doesn't compile with gcc, but does with Visual Studio: template <typename
The following code illustrates an object literal being assigned, but with no semicolon afterwards:
The following code should find the appropriate project tag and remove it from the
The following code: template <typename S, typename T> struct foo { void bar(); };

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.