Java uses the file. listfiles/list method to list objects in a directory. The following describes the usage of the file. listfiles method. The usage of file. List is also basically the same.
The file. listfiles method has three forms,
Public file [] listfiles ()
Listfiles without parameters will return all files, including sub-files and sub-directories.
Public file [] listfiles (Java. Io. filenamefilter)
Java. Io. filenamefilter: file name filter interface. The filter must implement this interface. This interface defines
Public Boolean accept (File file, string filename) method. The first parameter file is the file being filtered, and the second parameter is the file name being filtered. Filenamefilter. files that return false by accept will be filtered out.
This method returns a file that matches the conditions specified by filenamefilter.
Public file [] listfiles (Java. Io. filefilter)
Public Boolean accept (File file) method. The first parameter file is the file being filtered. Files whose filefilter. ACCEPT returns false will be filtered out.
This method returns a file that matches the conditions specified by the filefilter.
The following example illustrates the usage of the last two methods.
Obtain the list of objects with the specified extension:
Public static filenamefilter getfileextensionfilter (string extension ){
- Final string _ extension = extension;
- Return new filenamefilter (){
- Public Boolean accept (File file, string name ){
- Boolean ret = Name. endswith (_ extension );
- Return ret;
- }
- };
- }
- File file = new file ("C :\\");
- File [] zipfiles = file. listfiles (getfileextensionfilter (". Zip "));
public static FilenameFilter getFileExtensionFilter(String extension) { final String _extension = extension; return new FilenameFilter() { public boolean accept(File file, String name) { boolean ret = name.endsWith(_extension); return ret; } }; } File file = new File("c:\\"); File[] zipFiles = file.listFiles(getFileExtensionFilter(".zip"));
Obtains the list of objects whose names meet the specified rule expression.
Public static filenamefilter getfileregexfilter (string RegEx ){
- Final string RegEx _ = RegEx;
- Return new filenamefilter (){
- Public Boolean accept (File file, string name ){
- Boolean ret = Name. Matches (RegEx _);
- Return ret;
- }
- };
- }
- File file = new file ("C :\\");
- // The obtained file name is 8 numbers and the extended file name is .html
- File [] numberhtmlfiles = file. listfiles (getfileregexfilter ("[0-9] {8} \. html "));
Public static filenamefilter getfileregexfilter (string RegEx) {final string RegEx _ = RegEx; return New filenamefilter () {public Boolean accept (File file, string name) {Boolean ret = Name. matches (RegEx _); return ret ;};} file = new file ("C: \"); // extend the file named .html to file [] numberhtmlfiles = file. listfiles (getfileregexfilter ("[0-9] {8 }\\. html "));
Obtain the list of non-directory files:
Public static filefilter getnotdirfilefilter (){
- Return new filefilter (){
- Public Boolean accept (File file ){
- Return file. isfile ();
- }
- };
- }
- File file = new file ("C :\\");
- File [] notdirfiles = file. listfiles (getnotdirfilefilter ());