?
Solved

How to get all sub-directory and all files name in a given directory?

Posted on 2007-03-27
5
Medium Priority
?
1,901 Views
Last Modified: 2013-12-14
Given a specific directory, How to get all sub-directory and all files name in this  directory?
I am using java, thanks

eric
0
Comment
Question by:ericylr
  • 3
5 Comments
 
LVL 11

Expert Comment

by:Manish
ID: 18798424
0
 
LVL 11

Expert Comment

by:Manish
ID: 18798425
0
 
LVL 10

Accepted Solution

by:
Jaax earned 2000 total points
ID: 18798440
import java.io.File;
import java.util.ArrayList;

public class Main {
    private ArrayList searchFolders = new ArrayList();
    private ArrayList searchFiles = new ArrayList();

    public static void main(String[] args) {
        Main main = new Main();
        main.retrieveSearchFolders(new File("D:\\Temp")); //Specify ur directory
    }

    private void retrieveSearchFolders(File dir) {
        // System.out.println("Searching for Directory : " + dir);
        File[] files = dir.listFiles();
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                if (files[i].isDirectory()) {
                    System.out.println("Adding Folder : " + files[i]);
                    searchFiles.add(files[i]);
                    retrieveSearchFolders(files[i]);
                } else {
                    System.out.println("Adding File : " + files[i]);
                    searchFolders.add(files[i]);
                }
            }
        }
    }
}
0
 
LVL 11

Expert Comment

by:Manish
ID: 18798546
Oh..No help from my link?
0
 

Author Comment

by:ericylr
ID: 18807981
Karanw, sorry. i do not know how to assign the points to different people
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
Suggested Courses
Course of the Month15 days, 11 hours left to enroll

850 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question