The method of traversing files in a file object is basically the difference between the return values, a list () method that returns a string of strings, and a listfile () method that returns an array of the file objects. Or two methods with parameters, you can traverse a file of a specified type, such as a. mp3 file .... It is useful to filter the child files or sub-paths.
Public string[] List ()
Public string[] List (filenamefilter filter)
The filenamefilter here is public interface FilenameFilter the class instance implementing this interface is available for the filter file name. This implements the Accept (File dir, String name)
The method in which Dir-the directory where the found file resides. This is the path to the file object.
Name-Names of the files. The name of all files under the file object path.
Two parameters. This method iterates over the specified file subdirectory subfolder. If the method returns True, the list lists the subdirectory or subfolder.
Public classFilefiletertest { Public Static voidMain (string[] args) {//TODO auto-generated Method StubFile file=NewFile ("E://java's Jdk//lib"); String[] FileName=file.list (NewFileter ()); for(String string:filename) {System.out.println (string+" "); } }} /*** Implement Filenamefileter interface, implement the method of filtering files * If the method returns True, list lists the sub-file or subdirectory. * @authorCang **/ classFileterImplementsFilenameFilter {@Override//The name is the names of all the files or paths in file, the first level, and Dir is the absolute path to the document object. Public BooleanAccept (File dir, String name) {//TODO auto-generated Method Stub returnName.endswith (". Jar") | |NewFile (dir+ "//" +name). Isdirectory (); } }
Filters for Java files