[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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?
0
DevelHelper
Asked:
DevelHelper
  • 8
  • 4
  • 2
1 Solution
 
girionisCommented:
 If your path is: /home/develhelper/mydir try:

  File f = new File("/home/develhelper/mydir");
  String [] list = f.list();
0
 
DevelHelperAuthor Commented:
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?
0
 
António SargentoChief ArchitectCommented:
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).
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
DevelHelperAuthor Commented:
I tested the suggestion provided by girionis, but it doesn't return the files under subfolders.
0
 
girionisCommented:
>  could you please tell me how to get the subfolder names?

  They should be in the list with the files and subfolders returned.
0
 
DevelHelperAuthor Commented:
It returns the subfolder names, but it doesn't return the file names under subfolder.
0
 
DevelHelperAuthor Commented:
It returns the subfolder names, but it doesn't return the file names under subfolder.
0
 
António SargentoChief ArchitectCommented:
>  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
  }
0
 
DevelHelperAuthor Commented:
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?
0
 
António SargentoChief ArchitectCommented:
> One more question: how the method to return both the folder
> name and the file list?

I don't understand your answer...
However, to clarify my previous answer, here goes a small program that list a directory content recursively.

import java.io.*;

public class RecursiveList
{

  public RecursiveList()
  {}

  public void list(int tabs, File pathname)
  {
    for (int i=0;i<tabs;i++)
      System.out.print('\t');
    System.out.println(pathname.getName());
    if (pathname.isDirectory()) {
      File [] list = pathname.listFiles();
      for (int i =0;i<list.length;i++)
        list(tabs+1, list[i]);
    }
  }

  public static void main(String[] args)
  {
    RecursiveList recursiveList = new RecursiveList();
    recursiveList.list(0,new File(args[0]));
  }
}
0
 
DevelHelperAuthor Commented:
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.
0
 
DevelHelperAuthor Commented:
I think I need to create a java bean to store the subfolder name and all file names under that subfolder.
0
 
António SargentoChief ArchitectCommented:
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.
0
 
DevelHelperAuthor Commented:
Sorry, I didn't recognize this program already does that. You actually did an excellent job!
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 8
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now