Java - File list(FilenameFilter filter) method



Description

The Java File list(FilenameFilter filter) method returns the array of files and directories in the directory defined by this abstract path name that satisfy the given filter. All names are accepted if the given filter is null.

Declaration

Following is the declaration for java.io.File.list(FilenameFilter filter) method −

public String[] list(FilenameFilter filter)

Parameters

filter - to filter the list of files.

Return Value

The method returns the array of files and directories in the directory defined by this abstract path name that satisfy the given filter.

Exception

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

Example - Usage of File list(FilenameFilter filter) method

The following example shows the usage of Java File list(FilenameFilter filter) method.

We've created a filter class by implementing FilenameFilter 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 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.

FileDemo.java

package com.tutorialspoint;

import java.io.File;
import java.io.FilenameFilter;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String[] paths;

      try {    

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

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

         // array of files and directory
         paths = f.list(null);

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

               // prints filename and directory name
               System.out.println(path);
            }
         }else {
            System.out.println("Files are not present");
         }

      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

class TextFileFilter implements FilenameFilter{

   @Override
   public boolean accept(File dir, String name) {
      return name.endsWith("txt");
   }	
}

Output

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

test.txt
Test1.txt

Example - Usage of File list(FilenameFilter filter) method

The following example shows the usage of Java File list(FilenameFilter 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 files present in the test directory using list(FilenameFilter filter) method into a string 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.

FileDemo.java

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String[] paths;

      try {    

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

         // array of files and directory
         paths = f.list(null);

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

               // prints filename and directory name
               System.out.println(path);
            }
         }else {
            System.out.println("Files are not present");
         }

      } catch(Exception e) {
         // if any error occurs
         e.printStackTrace();
      }
   }
}

Output

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

test.pptx
test.txt
Test1.txt

Example - Usage of File list(FilenameFilter filter) method

The following example shows the usage of Java File list(FilenameFilter 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 list(filter) method into a string 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".

FileDemo.java

package com.tutorialspoint;

import java.io.File;

public class FileDemo {
   public static void main(String[] args) {      
      File f = null;
      String[] paths;

      try {    

         // create new file
         f = new File("F:/Test3");
		 
         // array of files and directory
         paths = f.list(null);

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

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

Output

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

Files are not present
java_io_file_methods.htm
Advertisements
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy