Link to home
Start Free TrialLog in
Avatar of DevelHelper
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?
Avatar of girionis
girionis
Flag of Greece image

 If your path is: /home/develhelper/mydir try:

  File f = new File("/home/develhelper/mydir");
  String [] list = f.list();
Avatar of DevelHelper
DevelHelper

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.FileSystemView).
- class File (java.io.File), that has methods that returns the files when that class represents a directory (the path name is a directory).
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.
It returns the subfolder names, but it doesn't return the file names under subfolder.
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/mydir");
File [] list = f.listFiles();

for (int i=0;i<list.length;i++)
  if (list[i].isDirectory())  { // is a subfolder
     File [] subfolders=list[i].listFiles();
     // show then... and check if any are a directory
  }
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?
ASKER CERTIFIED SOLUTION
Avatar of António Sargento
António Sargento
Flag of Portugal image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
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.
Sorry, I didn't recognize this program already does that. You actually did an excellent job!