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 2 of 2

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

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

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

    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.endsWith(".txt") || files.endsWith(".TXT"))
           {
              System.out.println(files);
            }
         }
      }
    }
    }

    You can modify the .txt or .TXT to be whatever file extension you wish.
    whilw running this program, i am getting null pointer exception..........
    can you tell me why?


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    18,616
    Thanks
    45
    Thanked 1,956 Times in 1,930 Posts

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

    Thread moved.

    i am getting null pointer exception
    Please copy the full text of the error message and paste it here.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Java program which can list all files in a given directory
    By JavaPF in forum Java Programming Tutorials
    Replies: 8
    Last Post: July 9th, 2013, 03:38 AM
  2. check if table exists from a list of files in a directory
    By efoikonom in forum JDBC & Databases
    Replies: 1
    Last Post: June 10th, 2013, 08:42 AM
  3. Replies: 1
    Last Post: May 25th, 2013, 05:13 AM
  4. to get name of all files from a particular directory to a list..
    By Himali in forum File I/O & Other I/O Streams
    Replies: 5
    Last Post: April 7th, 2012, 06:08 AM
  5. Java program which can list all files in a given directory
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 5
    Last Post: November 3rd, 2009, 08:42 AM