Results 1 to 5 of 5

Thread: Listing the Files or Subdirectories in a Directory

  1. #1
    Join Date
    Nov 2009
    Posts
    1,340

    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.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    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];
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    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);

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    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

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    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");
    
        }
    }

Similar Threads

  1. How to get the full path in directory listing
    By Ilampirai in forum Operating Systems
    Replies: 5
    Last Post: 13-04-2011, 10:25 AM
  2. Counting all files in a directory (and subdirectories)
    By Sarasija in forum Operating Systems
    Replies: 4
    Last Post: 22-08-2010, 01:03 AM
  3. Directory listing of a website
    By Gunner 1 in forum Software Development
    Replies: 5
    Last Post: 29-01-2010, 02:01 PM
  4. How to print Directory Listing in Windows
    By Foy in forum Windows Software
    Replies: 5
    Last Post: 14-09-2009, 02:55 PM
  5. Replies: 3
    Last Post: 29-06-2009, 06:58 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,036,716.65585 seconds with 17 queries