
Apache Commons IO - Utility Classes
- 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 - Filter Classes
- 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 - Comparator Classes
- Apache Commons IO - NameFileComparator
- Apache Commons IO - SizeFileComparator
- LastModifiedFileComparator
Apache Commons IO - Stream Classes
Apache Commons IO - Useful Resources
Apache Commons IO - NameFileFilter Class
Overview
NameFileFilter class in Commons IO filters the file-names for a name.
Class Declaration
Following is the declaration for org.apache.commons.io.filefilter.NameFileFilter Class :
public class NameFileFilter extends AbstractFileFilter implements Serializable
Here is the input file we need to parse −
input.txt
Welcome to TutorialsPoint. Simply Easy Learning.
Example - Printing all files in current directory.
CommonsIoTester.java
package com.tutorialspoint; import java.io.File; import java.io.IOException; import org.apache.commons.io.IOCase; import org.apache.commons.io.filefilter.NameFileFilter; public class CommonsIoTester { public static void main(String[] args) 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]); } } }
Output
It will print the following result −
All files and Folders. .classpath .project .settings bin input.txt output.txt src
Example - Filtering a file by name as Case Insensitive
CommonsIoTester.java
package com.tutorialspoint; import java.io.File; import java.io.IOException; import org.apache.commons.io.IOCase; import org.apache.commons.io.filefilter.NameFileFilter; public class CommonsIoTester { public static void main(String[] args) throws IOException { //get the current directory File currentDirectory = new File("."); System.out.println("\nFile with name input.txt\n"); String[] acceptedNames = {"input", "input.txt"}; String[] filesNames = currentDirectory.list( new NameFileFilter(acceptedNames, IOCase.INSENSITIVE) ); for( int i = 0; i < filesNames.length; i++ ) { System.out.println(filesNames[i]); } } }
Output
It will print the following result −
File with name input.txt input.txt
Example - Filtering a file by name as Case Sensitive
CommonsIoTester.java
package com.tutorialspoint; import java.io.File; import java.io.IOException; import org.apache.commons.io.IOCase; import org.apache.commons.io.filefilter.NameFileFilter; public class CommonsIoTester { public static void main(String[] args) throws IOException { //get the current directory File currentDirectory = new File("."); System.out.println("\nFile with name input.txt\n"); String[] acceptedNames = {"input", "input.txt"}; String[] filesNames = currentDirectory.list( new NameFileFilter(acceptedNames, IOCase.SENSITIVE) ); for( int i = 0; i < filesNames.length; i++ ) { System.out.println(filesNames[i]); } } }
Output
It will print the following result −
File with name input.txt input.txt
Advertisements