Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to get file name list?

Posted on 2003-03-03
14
Medium Priority
?
294 Views
Last Modified: 2010-03-31
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
Comment
Question by:DevelHelper
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 4
  • 2
14 Comments
 
LVL 35

Expert Comment

by:girionis
ID: 8057759
 If your path is: /home/develhelper/mydir try:

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

Author Comment

by:DevelHelper
ID: 8057817
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
 
LVL 2

Expert Comment

by:António Sargento
ID: 8057836
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
Build and deliver software with DevOps

A digital transformation requires faster time to market, shorter software development lifecycles, and the ability to adapt rapidly to changing customer demands. DevOps provides the solution.

 

Author Comment

by:DevelHelper
ID: 8057891
I tested the suggestion provided by girionis, but it doesn't return the files under subfolders.
0
 
LVL 35

Expert Comment

by:girionis
ID: 8057894
>  could you please tell me how to get the subfolder names?

  They should be in the list with the files and subfolders returned.
0
 

Author Comment

by:DevelHelper
ID: 8057930
It returns the subfolder names, but it doesn't return the file names under subfolder.
0
 

Author Comment

by:DevelHelper
ID: 8057945
It returns the subfolder names, but it doesn't return the file names under subfolder.
0
 
LVL 2

Expert Comment

by:António Sargento
ID: 8057953
>  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
 

Author Comment

by:DevelHelper
ID: 8058020
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
 
LVL 2

Accepted Solution

by:
António Sargento earned 150 total points
ID: 8058203
> 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
 

Author Comment

by:DevelHelper
ID: 8058298
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
 

Author Comment

by:DevelHelper
ID: 8058383
I think I need to create a java bean to store the subfolder name and all file names under that subfolder.
0
 
LVL 2

Expert Comment

by:António Sargento
ID: 8058398
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
 

Author Comment

by:DevelHelper
ID: 8058690
Sorry, I didn't recognize this program already does that. You actually did an excellent job!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question