How to list specific type of files in Java using FileFilter interface?

java.io.FileFilter is used to list specific type of files in a directory in Java. FileFilter is an interface found in java.io package that filters files depending on the file extension provided.

Introduction

A directory may contain variety of files, example: .xml files, .xslt files, .txt files, folders, .gif files etc. If you are interested in listing only say all the .xml files in this directory, the Java interface FileFilter can be used here.

I will give you a complete code at the end of the lesson so you can run the example and see how the functionality works, but before that I will explain the core bits of code required to execute this functionality.

 

Defining a FileFilter

In our example, we will try to list only the .xml files in the given directory. As FileFilter is an interface, we should implement a method called "accept()" which basically puts a condition to test if the specified file is an .xml file so that it can be included in the filtered list. 

// Defining a file filter and the condition that will satisfy the file filter
FileFilter xmlFilter = new FileFilter()
{
        public boolean accept(File file)
        {
                String sFilePath = file.getName();
                if( sFilePath.endsWith(".xml") )
                {
                        return true;
                }
                else
                {
                        return false;
                }
        }                      
};
 

Implementing the Filefilter was as simple as this, now the next step is to just use the filefilter to list our specific type of files.

 

Listing Files

File dir = new File("c:\project");

// List all the files in the given directory
File[] allTypesOfFiles = dir.listFiles();

// List only .xml files
File[] xmlFiles = dir.listFiles(xmlFilter);

The code above tells you how to retrieve all the files in the directory "c:\project" irrespective of the file type and to retrieve only xml files from that folder. listFiles() method returns an array of files requested. 

dir.listFiles(xmlFilter);

What happens at this point, is that it starts adding each file in the given directory to an array. Before adding, the accept() method in the FileFilter is executed where it checks if the current file is an xml file, i.e. if the path of the file ends with ".xml", if so then the file is added to the array.

 

 

Comments

How to list files recursively?

OK, but how to list files recursively?
Like in these tool: wList (Make a list of files and folders)
http://sharktime.com/us_wList.html

Regards,
Elizabeth

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options

How to list specific type of files in Java using FileFilter interface? | Our website now yours! - Currenlty Java focussed.

How to list specific type of files in Java using FileFilter interface?

java.io.FileFilter is used to list specific type of files in a directory in Java. FileFilter is an interface found in java.io package that filters files depending on the file extension provided.

Introduction

A directory may contain variety of files, example: .xml files, .xslt files, .txt files, folders, .gif files etc. If you are interested in listing only say all the .xml files in this directory, the Java interface FileFilter can be used here.

I will give you a complete code at the end of the lesson so you can run the example and see how the functionality works, but before that I will explain the core bits of code required to execute this functionality.

 

Defining a FileFilter

In our example, we will try to list only the .xml files in the given directory. As FileFilter is an interface, we should implement a method called "accept()" which basically puts a condition to test if the specified file is an .xml file so that it can be included in the filtered list. 

// Defining a file filter and the condition that will satisfy the file filter
FileFilter xmlFilter = new FileFilter()
{
        public boolean accept(File file)
        {
                String sFilePath = file.getName();
                if( sFilePath.endsWith(".xml") )
                {
                        return true;
                }
                else
                {
                        return false;
                }
        }                      
};
 

Implementing the Filefilter was as simple as this, now the next step is to just use the filefilter to list our specific type of files.

 

Listing Files

File dir = new File("c:\project");

// List all the files in the given directory
File[] allTypesOfFiles = dir.listFiles();

// List only .xml files
File[] xmlFiles = dir.listFiles(xmlFilter);

The code above tells you how to retrieve all the files in the directory "c:\project" irrespective of the file type and to retrieve only xml files from that folder. listFiles() method returns an array of files requested. 

dir.listFiles(xmlFilter);

What happens at this point, is that it starts adding each file in the given directory to an array. Before adding, the accept() method in the FileFilter is executed where it checks if the current file is an xml file, i.e. if the path of the file ends with ".xml", if so then the file is added to the array.

 

 

Comments

How to list files recursively?

OK, but how to list files recursively?
Like in these tool: wList (Make a list of files and folders)
http://sharktime.com/us_wList.html

Regards,
Elizabeth

Post new comment

  • You can enable syntax highlighting of source code with the following tags: <code>. Beside the tag style "<foo>" it is also possible to use "[foo]".
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
  • Search Engines will index and follow ONLY links to allowed domains.

More information about formatting options