Solved

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

Posted on 2007-03-27
5
1,896 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 500 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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

730 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