DevelHelper
asked on
How to get file name list?
My java application needs to get all file name list under a given path (includes files under subfolders) in server. Does someone know the solution?
ASKER
Thanks for your quick response. I'm going to test it. In the meantime, could you please tell me how to get the subfolder names?
There are two possibilities:
- class FileSystemView (javax.swing.filechooser.F ileSystemV iew).
- class File (java.io.File), that has methods that returns the files when that class represents a directory (the path name is a directory).
- class FileSystemView (javax.swing.filechooser.F
- class File (java.io.File), that has methods that returns the files when that class represents a directory (the path name is a directory).
ASKER
I tested the suggestion provided by girionis, but it doesn't return the files under subfolders.
> could you please tell me how to get the subfolder names?
They should be in the list with the files and subfolders returned.
They should be in the list with the files and subfolders returned.
ASKER
It returns the subfolder names, but it doesn't return the file names under subfolder.
ASKER
It returns the subfolder names, but it doesn't return the file names under subfolder.
> could you please tell me how to get the subfolder names?
Use the method listFiles() instead list() which returns File classes and the recursively list the Files that are directories(isDirectory method).
File f = new File("/home/develhelper/my dir");
File [] list = f.listFiles();
for (int i=0;i<list.length;i++)
if (list[i].isDirectory()) { // is a subfolder
File [] subfolders=list[i].listFil es();
// show then... and check if any are a directory
}
Use the method listFiles() instead list() which returns File classes and the recursively list the Files that are directories(isDirectory method).
File f = new File("/home/develhelper/my
File [] list = f.listFiles();
for (int i=0;i<list.length;i++)
if (list[i].isDirectory()) { // is a subfolder
File [] subfolders=list[i].listFil
// show then... and check if any are a directory
}
ASKER
Thanks! Amgs,
It seems that your answer is very close. One more question: how the method to return both the folder name and the file list?
It seems that your answer is very close. One more question: how the method to return both the folder name and the file list?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Good job!
But it would be great appreciated if you can tell how to know which files directly under the given dir and which files under subfolder name.
But it would be great appreciated if you can tell how to know which files directly under the given dir and which files under subfolder name.
ASKER
I think I need to create a java bean to store the subfolder name and all file names under that subfolder.
This program already does that, since on the first tab level are the files of the given directory and on the second tab level are the files in the sub folders.
If you want another kind of solution you should adapt this small example.
If you want another kind of solution you should adapt this small example.
ASKER
Sorry, I didn't recognize this program already does that. You actually did an excellent job!
File f = new File("/home/develhelper/my
String [] list = f.list();