First, Introduction
Many of the tools available in Apache are very useful and recommended.
Today in the use of the process of using the Org.apache.commons.io.FileUtils.listFiles method, this article mainly discuss the use of this tool method.
Check the instructions on the source is
/*** Finds files within a given directory (and optionally its * subdirectories). All files found is filtered by an iofilefilter. * <p> * If your search should recurse into subdirectories your can pass in * a iofilefilter for directories. You don ' t need to bind a * directoryfilefilter (via logical AND) to the this filter. This method does *. * <p> * Example:if want to search through all directories called * ' temp ' you pass in <code>fi Lefilterutils.namefilefilter ("temp") </code> * <p> * Another common usage of this method is find files In a directory * tree but ignoring the directories generated CVS. You can simply pass * in <code>filefilterutils.makecvsaware (null) </code>. * * @paramDirectory The directory to search in *@paramFileFilter Filter to apply when finding files. * @paramDirfilter Optional filter to apply when finding subdirectories. * If This parameter is {@codenull}, subdirectories won't is included in the * search. Use Truefilefilter.instance to match all directories. * @returnAn collection of java.io.File with the matching files *@seeOrg.apache.commons.io.filefilter.FileFilterUtils *@seeOrg.apache.commons.io.filefilter.NameFileFilter*/
The effect is:
finds files through the Iofilefilter filter in the specified directory (which can be assigned to subdirectories). For example: If you want to be in all directories named "Temp", you can use: Filefilterutils.namefilefilter ("temp")
Second, basic use
The directory structure under the test directory is as follows:
m:\filetest│ 5.txt├─001││ 1.txt││ 2.txt│││└─011│ bc.eddx│ d.docx│└─002 3.txt 4. Txt
At the beginning, just want to get the file under the directory, the following method:
collection<file> listfiles = fileutils.listfiles (newnullnull); for (File file:listfiles) { System.out.println (File.getname ()); }
An error is thrown here:
Its source code is as follows: Parameter ' filefilter ' is null "
Private Static voidValidatelistfilesparameters (FinalFile directory,Finaliofilefilter FileFilter) { if(!directory.isdirectory ()) { Throw NewIllegalArgumentException ("Parameter ' directory ' is not a directory:" +directory); } if(FileFilter = =NULL) { Throw NewNullPointerException ("Parameter ' filefilter ' is null"); } }
As you can see, the second filter for a file cannot be empty.
Change to the second approach: Create a file filter with Filefilterutils. The following code uses Filefilterutils.suffixfilefilter ("TXT") to filter out files with the file name suffix txt, and the third parameter indicates whether to recursively query the directory, or null for recursion.
@Test Public voidtest2 () {Collection<File> listfiles = Fileutils.listfiles (NewFile ("M:/filetest"), Filefilterutils.suffixfilefilter ("TXT"),NULL); Showfiles (Listfiles); } Private voidShowfiles (collection<file>listfiles) { if(listfiles==NULL) { return; } for(File file:listfiles) {System.out.println (File.getname ()); } }
The results of the above method are:
5.txt
Modify the last parameter slightly, converting it from null to Directoryfilefilter.instance, and the method will use recursion to scan the file for filtering.
@Test publicvoid test3 () { Collection<File> listfiles = Fileutils.listfiles (new File ("M:/filetest"), Filefilterutils.suffixfilefilter ("TXT"), directoryfilefilter.instance); Showfiles (listfiles); }
The result is:
1.txt2.txt 3. txt 4.txt5.txt
It is important to note that, if only the filter suffix, Apache provides a simpler method:
@Test publicvoid test4 () { Collection<File> listfiles = Fileutils.listfiles (newnewtrue); Showfiles (listfiles); }
Method description See, here time relationship is not repeat.
Iii. Common File Filters
Some information can be obtained from one of the most flexible method signatures:
The second parameter is Iofilfilter FileFilter, which can be known as an interface by looking at the source code form.
Obviously we look at what implementations of this interface can be obtained by those filefilter.
From the above figure of the demerit can be seen based on the file size of the Sizefilefilter, based on the file name prefix Prefixfilefilter ..., the specific use of the situation, according to their own project situation selection, I do not give an example here (0.0 actually I did not use all the ~ ~ ~)
@Test publicvoid test5 () { Collection<File> listfiles = Fileutils.listfiles (new File ("M:/filetest"), Filefilterutils.and (Emptyfilefilter.not_empty,new Regexfilefilter ("^[0-9]+.[ a-za-z]+$ ")), directoryfilefilter.instance); Showfiles (listfiles); }
Because my directory has only 5. txt a bit of content, so the final result is 5.txt, the above code note
Filefilterutils.and (Emptyfilefilter.not_empty,new regexfilefilter ("^[0-9]+.[ a-za-z]+$ "))
There are 2 file filters, one of which is a non-empty file filter, and the other is a filter to find the file name, and the matching rule is: "^[0-9]+. [a-za-z]+$] The file name can only be one or more digits, and the suffix is composed of one or more letters.
~~over
This blog post is my best intentions of the first time ....
Fileutils.listfiles methods in Apache simple tips for use