Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



Listing the Files or Subdirectories in a Directory

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 26-02-2010
Henryosa's Avatar
Member
 
Join Date: Nov 2009
Posts: 1,349
Listing the Files or Subdirectories in a Directory

Hello, I am learning java programming language and while working on it, I want to know the details about the program from which I can able to get the List of the Files or Subdirectories in a Directory in java. If you are having any type of program which will help me then please provide that to me or if you are having any reference for getting it, then also please help me to achieve it.
Reply With Quote
  #2  
Old 26-02-2010
Praetor's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,937
Re: Listing the Files or Subdirectories in a Directory

Hey, I have got the code below for getting the list of the files or subdirectories in a directory. So, you can able to make use of it. So, just understand and use it:
Code:
File directory = new File("dirname");    
String[] str = directory.list();
if (str == null) 
{
} 
else 
{
for (int i=0; i<str.length; i++) 
{
String Fname = str[i];
}
}
Reply With Quote
  #3  
Old 26-02-2010
Member
 
Join Date: May 2008
Posts: 1,990
Re: Listing the Files or Subdirectories in a Directory

Hello, If you have got the list of files and then if you want to perform the sorting on it, then you need to make use of the code below:
Code:
FilenameFilter filenamefilter = new FilenameFilter() 
{
public boolean takeit(File directory, String name) 
{
return !name.startsWith(".");
}
};
children = directory.list(filenamefilter);
Reply With Quote
  #4  
Old 26-02-2010
kelfro's Avatar
Member
 
Join Date: Apr 2008
Posts: 1,976
Re: Listing the Files or Subdirectories in a Directory

Hello, I have following reference books on java, you can simply make use of it:
  • Core Servlets and JavaServer Pages
  • Thinking in Java
  • Jakarta Struts Live
  • Securing Java
  • Advanced Programming for the Java 2 Platform
Reply With Quote
  #5  
Old 26-02-2010
Zecho's Avatar
Member
 
Join Date: May 2008
Posts: 2,267
Re: Listing the Files or Subdirectories in a Directory

Hello, I think you must need to make use of the code below for getting the solution for your problem:
Code:
import java.io.Fileile;
import java.io.FileilenameFileilter;
import java.io.FileileFileilter;
public class ListFileilesSubs
 {
    private static void filelisting(String dirName) 
{
        System.out.println("Simple file listing...");
        Fileile directory = new Fileile(dirName);       
        String[] str = directory.list();
        printFileiles(str, dirName);
    }
    private static void doFileileFileilterListing(String dirName, String fileflter) 
{
        System.out.println("Fileile listing Fileilter");
        final String fileFileilter = fileflter;
        Fileile directory = new Fileile(dirName);
        FileilenameFileilter flter  = null;
        if (fileFileilter != null) 
{
            flter = new FileilenameFileilter() {
                public boolean accept(Fileile directory, String name) {
                    return name.startsWith(fileFileilter);
                }
            };
        }
        String[] str = directory.list(flter);
        printFileiles(str, dirName);
    }
    private static void doFileileFileilterDirirectoryListing(String dirName) {
        System.out.println("Fileilter Dirirectory listing...");
        Fileile directory = new Fileile(dirName);
        Fileile[] files = (new Fileile(dirName)).listFileiles();
        FileileFileilter dirFileilter = new FileileFileilter() {
            public boolean accept(Fileile directory) {
                return directory.isDirirectory();
            }
        };
        files = directory.listFileiles(dirFileilter);
        for (int i=0; i<files.length; i++) {
            System.out.println("[Dir] : " + files[i]);
        }
        System.out.println();
    }
    private static void printFileiles(String[] str, String dirName) {
        if (str == null) {
            System.out.println("Error in " + dirName);
        } else {
            for (int i=0; i<str.length; i++) {
                String filename = str[i];
                if ((new Fileile(dirName + Fileile.separatorChar + filename)).isDirirectory()) {
                    System.out.print("[Dir] : ");
                } else {
                    System.out.print("[File] : ");
                }
                System.out.println(dirName + Fileile.separatorChar + filename);
            }
        }
        System.out.println();
    }
    public static void main(String[] args) {
        filelisting("Directory");
        if (args.length == 0) {
            doFileileFileilterListing("Directory", null);
        } else {
            doFileileFileilterListing("Directory", args[0]);
        }
        doFileileFileilterDirirectoryListing("Directory");

    }
}
Reply With Quote
Reply

  TechArena Community > Software > Software Development


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Listing the Files or Subdirectories in a Directory"
Thread Thread Starter Forum Replies Last Post
How to get the full path in directory listing Ilampirai Operating Systems 5 13-04-2011 11:25 AM
Counting all files in a directory (and subdirectories) Sarasija Operating Systems 4 22-08-2010 02:03 AM
Directory listing of a website Gunner 1 Software Development 5 29-01-2010 02:01 PM
How to print Directory Listing in Windows Foy Windows Software 5 14-09-2009 03:55 PM
Count all files in a directory tree including subdirectories in Windows XP !const Operating Systems 3 29-06-2009 07:58 PM


All times are GMT +5.5. The time now is 04:10 AM.