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

Program Output

Output of the program will look like this:

All Types of Files:
c:\projects\flowers.gif
c:\projects\error.log
c:\projects\z.src
c:\projects\start.xml
c:\projects\end.xml
c:\projects\documentation.txt

XML Files:
c:\projects\start.xml
c:\projects\end.xml

TEXT Files:
c:\projects\documentation.txt


Example Program: FilterFiles.java

// Java class FilterFiles
import java.io.File;
import java.io.FileFilter;

public class FilterFiles
{
        // StringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to String. constants - file extensions
        public static final String EXT_XML = ".xml";
        public static final String EXT_TXT = ".txt";
       
        protected FileFilter xmlFilter = null;
        protected FileFilter txtFilter = null;

        public FilterFiles()
        {
                // Create a file filter to locate only xml files
                xmlFilter = new FileFilter()
                {
                        public boolean accept(File file)
                        {
                                String sPath = file.getName();
                               
                                if( sPath.endsWith(EXT_XML) )
                                {
                                        return true;
                                }
                                else
                                {
                                        return false;
                                }
                        }      
                };
               
                // Create a file filter to locate only text files
                txtFilter = new FileFilter()
                {
                        public boolean accept(File file)
                        {
                                String sPath = file.getName();
                               
                                if( sPath.endsWith(EXT_TXT) )
                                {
                                        return true;
                                }
                                else
                                {
                                        return false;
                                }
                        }      
                };

        }
       
        public void getFiles(String dirPath)
        {
                File dir = new File(dirPath);
                if(dir.exists())
                {
                        // list all types of files
                        File[] allFiles = dir.listFiles();
                        if(allFiles != null)
                        {
                                // Print them out in the console
                                System.out.println("All Types of Files: ");
                                for(int n = 0; n < allFiles.length; n++)
                                {
                                        System.out.println(allFiles[n]);
                                }
                        }
                        else
                        {
                                System.out.println("No files in folder: "+ dirPath);
                        }
                       
                        // list only xml files
                        File[] xmlFiles = dir.listFiles(xmlFilter);
                        if(xmlFiles != null)
                        {
                                // Print them out in the console
                                System.out.println("XML Files: ");
                                for(int n = 0; n < xmlFiles.length; n++)
                                {
                                        System.out.println(xmlFiles[n]);
                                }
                        }
                        else
                        {
                                System.out.println("No xml files in folder: "+ dirPath);
                        }
                       
                        // list only text files
                        File[] txtFiles = dir.listFiles(txtFilter);
                        if(txtFiles != null)
                        {
                                // Print them out in the console
                                System.out.println("TEXT Files: ");
                                for(int n = 0; n < txtFiles.length; n++)
                                {
                                        System.out.println(txtFiles[n]);
                                }
                        }
                        else
                        {
                                System.out.println("No text files in folder: "+ dirPath);
                        }
                       
                }
                else
                {
                        System.out.println("Folder "+ dirPath + " does not exist!");
                }
        }

        /**
         * @param args
         */

        public static void main(String[] args)
        {
                FilterFiles object = new FilterFiles();
                object.getFiles("c:/projects");        
        }

} // End of class

 

 

Comments

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

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

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

Program Output

Output of the program will look like this:

All Types of Files:
c:\projects\flowers.gif
c:\projects\error.log
c:\projects\z.src
c:\projects\start.xml
c:\projects\end.xml
c:\projects\documentation.txt

XML Files:
c:\projects\start.xml
c:\projects\end.xml

TEXT Files:
c:\projects\documentation.txt


Example Program: FilterFiles.java

// Java class FilterFiles
import java.io.File;
import java.io.FileFilter;

public class FilterFiles
{
        // StringThe String class represents character strings. All string literals in Java programs, such as "abc", are implemented as instances of this class. Below are all the articles related to String. constants - file extensions
        public static final String EXT_XML = ".xml";
        public static final String EXT_TXT = ".txt";
       
        protected FileFilter xmlFilter = null;
        protected FileFilter txtFilter = null;

        public FilterFiles()
        {
                // Create a file filter to locate only xml files
                xmlFilter = new FileFilter()
                {
                        public boolean accept(File file)
                        {
                                String sPath = file.getName();
                               
                                if( sPath.endsWith(EXT_XML) )
                                {
                                        return true;
                                }
                                else
                                {
                                        return false;
                                }
                        }      
                };
               
                // Create a file filter to locate only text files
                txtFilter = new FileFilter()
                {
                        public boolean accept(File file)
                        {
                                String sPath = file.getName();
                               
                                if( sPath.endsWith(EXT_TXT) )
                                {
                                        return true;
                                }
                                else
                                {
                                        return false;
                                }
                        }      
                };

        }
       
        public void getFiles(String dirPath)
        {
                File dir = new File(dirPath);
                if(dir.exists())
                {
                        // list all types of files
                        File[] allFiles = dir.listFiles();
                        if(allFiles != null)
                        {
                                // Print them out in the console
                                System.out.println("All Types of Files: ");
                                for(int n = 0; n < allFiles.length; n++)
                                {
                                        System.out.println(allFiles[n]);
                                }
                        }
                        else
                        {
                                System.out.println("No files in folder: "+ dirPath);
                        }
                       
                        // list only xml files
                        File[] xmlFiles = dir.listFiles(xmlFilter);
                        if(xmlFiles != null)
                        {
                                // Print them out in the console
                                System.out.println("XML Files: ");
                                for(int n = 0; n < xmlFiles.length; n++)
                                {
                                        System.out.println(xmlFiles[n]);
                                }
                        }
                        else
                        {
                                System.out.println("No xml files in folder: "+ dirPath);
                        }
                       
                        // list only text files
                        File[] txtFiles = dir.listFiles(txtFilter);
                        if(txtFiles != null)
                        {
                                // Print them out in the console
                                System.out.println("TEXT Files: ");
                                for(int n = 0; n < txtFiles.length; n++)
                                {
                                        System.out.println(txtFiles[n]);
                                }
                        }
                        else
                        {
                                System.out.println("No text files in folder: "+ dirPath);
                        }
                       
                }
                else
                {
                        System.out.println("Folder "+ dirPath + " does not exist!");
                }
        }

        /**
         * @param args
         */

        public static void main(String[] args)
        {
                FilterFiles object = new FilterFiles();
                object.getFiles("c:/projects");        
        }

} // End of class

 

 

Comments

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