Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: Java program which can list all files in a given directory

  1. #1
    mmm.. coffee JavaPF's Avatar
    Join Date
    May 2008
    Location
    United Kingdom
    Posts
    3,336
    My Mood
    Mellow
    Thanks
    258
    Thanked 294 Times in 227 Posts
    Blog Entries
    4

    Default Java program which can list all files in a given directory

    With this code you can list all files in a given directory:

    import java.io.File;
     
    public class ListFiles 
    {
     
     public static void main(String[] args) 
    {
     
      // Directory path here
      String path = "."; 
     
      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(); 
     
      for (int i = 0; i < listOfFiles.length; i++) 
      {
     
       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
       System.out.println(files);
          }
      }
    }
    }

    If you want to list only .txt files for example, Use this code:

    import java.io.File;
     
    public class ListFiles 
    {
     
     public static void main(String[] args) 
    {
     
      // Directory path here
      String path = "."; 
     
      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(); 
     
      for (int i = 0; i < listOfFiles.length; i++) 
      {
     
       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
           if (files.endsWith(".txt") || files.endsWith(".TXT"))
           {
              System.out.println(files);
            }
         }
      }
    }
    }

    You can modify the .txt or .TXT to be whatever file extension you wish.

  2. The Following User Says Thank You to JavaPF For This Useful Post:

    aaronjsmith21 (August 16th, 2012)


  3. #2
    Junior Member Ciwan's Avatar
    Join Date
    Feb 2009
    Posts
    14
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to List all files in a directory

    This might come in handy one day !

    Thanks JavaPF
    "Extraordinary Claims Require Extraordinary Evidence" .... American Astrochemist Carl Sagan.

  4. #3
    Member Fendaril's Avatar
    Join Date
    Nov 2008
    Posts
    54
    Thanks
    7
    Thanked 6 Times in 5 Posts

    Default Re: How to List all files in a directory

    Very clever indeed.

  5. #4
    Junior Member
    Join Date
    Sep 2009
    Posts
    9
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: How to List all files in a directory

    Thx !!! Nice Info !!!

  6. #5
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,896
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: How to List all files in a directory

    Alternative to test for files with a certain ending:

    MyFilter implements FileFilter
    {
         public boolean accepts(File pathname)
         {
              if (pathname.endswith(".txt") || pathname.endswith(".TXT"))
              {
                   return true;
              }
              return false;
         }
    }

    import java.io.File;
     
    public class ListFiles 
    {
     
     public static void main(String[] args) 
    {
     
      // Directory path here
      String path = "."; 
     
      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(new MyFilter()); 
     
      for (int i = 0; i < listOfFiles.length; i++) 
      {
              System.out.println(listOfFiles[i].getName());
      }
    }
    }

    technically, this code will display folders named hello.txt, or this is a directory.txt, but I think it's fairly safe to assume that most people won't name their directories this way.

  7. #6
    Junior Member
    Join Date
    Oct 2009
    Posts
    19
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to List all files in a directory

    good job friends.

    the code which is written by javapf is
    can be modify
    i think the if code totlly write into a function
    we will display the files if it is a file other wise we call recursively that function again
    we can get all files and folders recursivly with that method.
    bye guys.

  8. #7
    Junior Member
    Join Date
    Aug 2012
    Posts
    1
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: How to List all files in a directory

    I know I am fairly new to Developing with java but I feel you could change the line
    if (files.endsWith(".txt") || files.endsWith(".TXT"))
    with
    if (files.toLowerCase().endsWith(".txt"))
    and this way you can exclude the or statement ( || ) and allow for any possible variation in the extension, .txt / .Txt / .tXt / .tXT / .TXT / .TXt (I think that is all), I think it will make for cleaner and more defined code, and that way no matter how ever the casing is you can still get all the possible files with that extension. Let me know if I am wrong...But I love the example... Below is the changed code:

    Quote Originally Posted by JavaPF View Post
    With this code you can list all files in a given directory:

    import java.io.File;
     
    public class ListFiles 
    {
     
     public static void main(String[] args) 
    {
     
      // Directory path here
      String path = "."; 
     
      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(); 
     
      for (int i = 0; i < listOfFiles.length; i++) 
      {
     
       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
       System.out.println(files);
          }
      }
    }
    }

    If you want to list only .txt files for example, Use this code:

    import java.io.File;
     
    public class ListFiles 
    {
     
     public static void main(String[] args) 
    {
     
      // Directory path here
      String path = "."; 
     
      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(); 
     
      for (int i = 0; i < listOfFiles.length; i++) 
      {
     
       if (listOfFiles[i].isFile()) 
       {
       files = listOfFiles[i].getName();
           if (files.toLowerCase().endsWith(".txt"))
           {
              System.out.println(files);
            }
         }
      }
    }
    }

    You can modify the .txt to be whatever file extension you wish.

  9. #8
    Junior Member
    Join Date
    Jul 2013
    Posts
    4
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    Quote Originally Posted by helloworld922 View Post
    import java.io.File;
     
    public class ListFiles 
    {
     
     public static void main(String[] args) 
    {
     
      // Directory path here
      String path = "."; 
     
      String files;
      File folder = new File(path);
      File[] listOfFiles = folder.listFiles(new MyFilter()); 
     
      for (int i = 0; i < listOfFiles.length; i++) 
      {
              System.out.println(listOfFiles[i].getName());
      }
    }
    }
    Do you have to give your new filter() a value? I mean, how does java know what you want to filter if you just say myfilter..

  10. #9
    Senior Member PhHein's Avatar
    Join Date
    Mar 2013
    Location
    Germany
    Posts
    609
    My Mood
    Sleepy
    Thanks
    10
    Thanked 93 Times in 86 Posts

    Default Re: Java program which can list all files in a given directory

    Stayawake, stay awake and have a look at the timestamps. This thread is years old.
    MyFilter's implementation knows it.

  11. The Following User Says Thank You to PhHein For This Useful Post:

    Stayawake (July 10th, 2013)

Similar Threads

  1. How to create directory in Java?
    By JavaPF in forum Java Programming Tutorials
    Replies: 2
    Last Post: December 24th, 2011, 12:53 PM
  2. Program to print current directory path to the console
    By JavaPF in forum Java Programming Tutorials
    Replies: 1
    Last Post: October 9th, 2009, 12:59 PM
  3. getting files in the linux server
    By Truffy in forum Java Networking
    Replies: 36
    Last Post: June 22nd, 2009, 06:32 PM
  4. Recursive function based on Linked list
    By rosh72851 in forum Collections and Generics
    Replies: 1
    Last Post: March 9th, 2009, 06:23 PM
  5. FileNotFound error while working with XML files in windows
    By ochieng in forum File I/O & Other I/O Streams
    Replies: 1
    Last Post: November 14th, 2008, 07:56 AM