- Apache Commons IO Tutorial
- Apache Commons IO - Home
- Apache Commons IO - Overview
- Apache Commons IO - Environment Setup
- Apache Commons IO - IOUtils
- Apache Commons IO - FileUtils
- Apache Commons IO - FilenameUtils
- Apache Commons IO - FileSystemUtils
- Apache Commons IO - IOCase
- Apache Commons IO - LineIterator
- Apache Commons IO - NameFileFilter
- Apache Commons IO - WildcardFileFilter
- Apache Commons IO - SuffixFileFilter
- Apache Commons IO - PrefixFileFilter
- Apache Commons IO - OrFileFilter
- Apache Commons IO - AndFileFilter
- Apache Commons IO - FileEntry
- Apache Commons IO - FileAlterationObserver
- Apache Commons IO - FileAlterationMonitor
- Apache Commons IO - NameFileComparator
- Apache Commons IO - SizeFileComparator
- LastModifiedFileComparator
- Apache Commons IO - TeeInputStream
- Apache Commons IO - TeeOutputStream
- Apache Commons IO - Useful Resources
- Apache Commons IO - Quick Guide
- Apache Commons IO - Useful Resources
- Apache Commons IO - Discussion
Apache Commons IO - SuffixFileFilter
SuffixFileFilter filters the files which are based on suffix. This is used in retrieving all the files of a particular type.
Class Declaration
Following is the declaration for org.apache.commons.io.filefilter.SuffixFileFilter Class −
public class SuffixFileFilter extends AbstractFileFilter implements Serializable
Example of SuffixFileFilter Class
Here is the input file we need to parse −
Welcome to TutorialsPoint. Simply Easy Learning.
Let's print all files and directories in the current directory and then, filter a file with extension txt.
IOTester.java
import java.io.File; import java.io.IOException; import org.apache.commons.io.filefilter.SuffixFileFilter; public class IOTester { public static void main(String[] args) { try { usingSuffixFileFilter(); } catch(IOException e) { System.out.println(e.getMessage()); } } public static void usingSuffixFileFilter() throws IOException { //get the current directory File currentDirectory = new File("."); //get names of all files and directory in current directory String[] files = currentDirectory.list(); System.out.println("All files and Folders.\n"); for( int i = 0; i < files.length; i++ ) { System.out.println(files[i]); } System.out.println("\nFile with extenstion txt\n"); String[] filesNames = currentDirectory.list( new SuffixFileFilter("txt")); for( int i = 0; i < filesNames.length; i++ ) { System.out.println(filesNames[i]); } } }
Output
It will print the following result.
All files and Folders. .classpath .project .settings bin input.txt src File with extenstion txt input.txt
Advertisements