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

In my project I have the following helper method, which goes through all the
// following code works fine n open notepad... class demo { public static void
I'm basically running the following code. This code goes through line by line and
After getting the objectEnumerator in the following code, the set1 retain count goes to
I have the following line of code which when compiled with GHC it goes
I run the following codes separately as my prompt unsuccessfully in .zshrc. This suggests
I can use the following codes to trim a string: -(void) aMethod { //
I've always used the following codes to create a folder on a Apache server
Following is some codes and output from the Chrome Developers' Console Case 1: var
I'm basically asking why the following lines of codes do not compile: type IGenericType<'a>

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.