Java - File listFiles() Method



Description

The Java File listFiles(FileFilter filter) returns an array of abstract pathnames indicating the files and directories in the directory indicated by this abstract pathname that satisfy the specified filter.

Declaration

Following is the declaration for java.io.File.listFiles(FileFilter filter) method −

public File[] listFiles(FileFilter filter)

Parameters

filter − File filter

Return Value

The method returns an array of abstract pathnames indicating the files and directories in the directory indicated by this abstract pathname that satisfy the specified filter.

Exception

SecurityException − If a security manager exists and its SecurityManager.checkRead(java.lang.String) method denies read access to the file

Example 1

The following example shows the usage of Java File listFiles(filter) method. We've created a filter class by implementing FileFilter class. This class is implementing the accept method and returning true if file name is ending with "txt". We'll be using this class in our program to filter text files. We've created a File reference. Then we're creating a File Object using test directory which is present in the provided directory. Then we're getting the list of text files present in the test directory using list(filter) method into a string array. Then this array is iterated to print the available files in the given directory.

package com.tutorialspoint;
import java.io.File;
import java.io.FileFilter;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File[] paths;

      try {    

         // create new file
         f = new File("F:/Test2");

         // create new filter
         FileFilter filter = new TextFileFilter();

         // array of files and directory
         paths = f.listFiles(filter);

         if(paths != null) {
            // for each name in the path array
            for(File path:paths) {

               // prints filename and directory name
               System.out.println(path.getName());
            }
         }else {
             System.out.println("Files are not present");
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

class TextFileFilter implements FileFilter{

   @Override
   public boolean accept(File pathname) {
      return pathname.getName().endsWith(".txt");
   }	
}

Output

Let us compile and run the above program, this will produce the following result −

test.txt
Test1.txt

Example 2

The following example shows the usage of Java File listFiles(filter) method. We've created a File reference. Then we're creating a File Object using test directory which is present in the provided directory. Then we're getting the list of all files present in the test directory using listFiles(filter) method into a File array. We've passed filter as null so that all files comes. Then this array is iterated to print the available files in the given directory.

package com.tutorialspoint;
import java.io.File;
import java.io.FileFilter;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File[] paths;

      try {    

         // create new file
         f = new File("F:/Test2");

         FileFilter filter = null;

         // array of files and directory
         paths = f.listFiles(filter);

         if(paths != null) {
            // for each name in the path array
            for(File path:paths) {

               // prints filename and directory name
               System.out.println(path.getName());
            }
         }else {
             System.out.println("Files are not present");
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

class TextFileFilter implements FileFilter{

   @Override
   public boolean accept(File pathname) {
      return pathname.getName().endsWith(".txt");
   }	
}

Output

Let us compile and run the above program, this will produce the following result −

test.pptx
test.txt
Test1.txt

Example 3

The following example shows the usage of Java File listFiles(filter) method. We've created a File reference. Then we're creating a File Object using test3 directory which is not present in the provided directory. Then we're getting the list of files present in the test directory using listFiles() method into a File array. Then this array is iterated to print the available files in the given directory. As folder is not present, array will be null and we're printing a message - "Files are not present".

package com.tutorialspoint;
import java.io.File;
import java.io.FileFilter;
public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      File[] paths;

      try {    

         // create new file
         f = new File("F:/Test3");

         FileFilter filter = null;

         // array of files and directory
         paths = f.listFiles(filter);

         if(paths != null) {
            // for each name in the path array
            for(File path:paths) {

               // prints filename and directory name
               System.out.println(path.getName());
            }
         }else {
             System.out.println("Files are not present");
         }
      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}
class TextFileFilter implements FileFilter{
   @Override
   public boolean accept(File pathname) {
      return pathname.getName().endsWith(".txt");
   }	
}

Output

Let us compile and run the above program, this will produce the following result −

Files are not present
java_file_class.htm
Advertisements