Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

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

Threaded View

  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)


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