File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Java in General and the fly likes Getting A File List From A Directory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Getting A File List From A Directory" Watch "Getting A File List From A Directory" New topic

Getting A File List From A Directory

Alan Jackson

Joined: Oct 26, 2004
Posts: 26
Okay, here is my problem. I'm trying to develop a small RMI application which will look in the folder "c:/alanistic" and produce an object array called "fileList" which will hold the names of all of the files in the directory. What I have been trying is:

File initialDir = new File("C:/comu303ass1");

public static void listFiles(File pInitialDir) {
File[] fileList = pInitialDir.listFiles();
for (int i = 0; i < files.length; i++) {
String fileName = fileList[i].getName();

Basically my app will act as a server, so when a client connects it will list all the files in the clients folder.

Any info is appreciated.
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
If what you want is just the names, why not use the list() method? The only reason I can think of to use listFiles() is so you can apply a FileFilter.
Michael Herrmann
Ranch Hand

Joined: Dec 06, 2003
Posts: 60
I wrote a little class and got it finished with the help of other form members that might help you(incl. recursively searching through subdirectories of subdirectories of subdirectories of...).
You can find it here

Michael Herrmann
[ October 26, 2004: Message edited by: Michael Herrmann ]
I agree. Here's the link:
subject: Getting A File List From A Directory
jQuery in Action, 3rd edition