Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

folders and files 1

Status
Not open for further replies.

secondleft

Programmer
Apr 11, 2006
14
GB
Hi,

Can you show me the code to list all sub-folders and files under a given folder?
Better not use recursive program but if this way is simpler then it is OK.

Thank you in advance.
 
You can use File class to do this.

Quick and dirty and not checked (should work somewhat like this):

Code:
File file = new File(YOUR_PATH);

String[] list = file.list(); // Returns list of files as string array

Check File in die API
 
Thank you.

How about the sub-folders? Will they be listed in the String array? If they will, how to tell which is file and witch is fub-folder?
 
You can use the isDirectory() or isFile() methods to check for file or folder.

Actually listFiles() will work better than list() because it will return an array of File, which you can then use.

 
Thanks a lot! I think those are what I need to make my code.
 
The most efficient way to list all files and subfolders, and files in those subfolders is to use recursion.

Look at the File API documentation (isFile(), isDirectory() calls)

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top