1. First look at a simple example of a file class.
Package test; import Java. io. file; import Java. io. filenamefilter; import Java. util. arrays; import Java. util. imports; import Java. util. regEx. pattern;/*** file indicates Files And Directories. static fields include system-related path delimiters and system-related default name delimiters. * Main operations include: create a file or directory, delete a file, Set Properties for the file, return the file list under the specified directory, * return the filtered file list, check whether the file exists, hide it, whether it is a directory or not file, * returned file name and path ** @ author touch **/public class filedemo {/** find the file matching RegEx in the specified path */Public String [] Find (string path, final string RegEx) {file = new file (PATH); // return file, an anonymous internal class. list (New filenamefilter () {private pattern = pattern. compile (RegEx); @ overridepublic Boolean accept (File Dir, string name) {// todo auto-generated method stubreturn pattern. matcher (name ). matches () ;}});} public static void main (string [] ARGs) {string Path = NULL; string key = NULL; string RegEx = NULL; int choice = 1; required bytes = new bytes (system. in); system. out. println ("Please input the file path:"); Path = inputs. next (); system. out. println ("Please Input key:"); Key = inputs. next (); system. out. println ("choise: \ N0: matching files suffixed with" + key + "\ N1: matching files containing" + key + "); if (choice = response. nextint () = 0) RegEx = ". *\\. "+ key; elseregex = ". * "+ key + ". * "; string [] list; List = new filedemo (). find (path, RegEx); system. out. println (arrays. deeptostring (list); // returns the directory list file [] filelist = new file (PATH) in the specified path ). listfiles (); For (File file: filelist) {If (file. isdirectory () {list = new filedemo (). find (file. getpath (), RegEx); system. out. println (arrays. deeptostring (list ));}}}}
Hosts file)
Package mine. util; import Java. io. file; import Java. util. arraylist; import Java. util. list; /*** the filedirectory class is used to find all files and directories under the specified root directory. You can use regular expressions to filter the files and directories to be searched. ** @ author touch */public final class searchfile {// Private list of stored files <File> filelist = new arraylist <File> (); // storage directory private list <File> directorylist = new arraylist <File> (); // storage file and directory private list <File> List = new arraylist <File> (); private file; // directory private string RegEx; // Regular Expression public searchfile (string path) {file = new file (PATH); this. regEx = ". * ";} public searchfile (File file) {This. file = file; this. regEx = ". * ";} public searchfile (string path, string RegEx) {file = new file (PATH); this. regEx = RegEx;} public searchfile (File file, string RegEx) {This. file = file; this. regEx = RegEx;} // return all files and subdirectories in the current directory public list <File> files () {file [] files = file. listfiles (); List <File> List = new arraylist <File> (); For (file F: Files) if (F. getname (). matches (RegEx) list. add (f); return list;} // return all the public list <File> allfiles () {If (list. isempty () Search (File); Return filelist;} // return all subdirectories under the root directory public list <File> alldirectory () {If (list. isempty () Search (File); Return directorylist;} // return all files and subdirectories under the root directory public list <File> allfilesanddirectory () {If (list. isempty () Search (File); return list;} // recursively searches all files in the current directory and private void search (File file) {file [] files = file. listfiles (); If (Files = NULL | files. length = 0) return; For (file F: Files) {If (F. getname (). matches (RegEx) list. add (f); If (F. isfile () & F. getname (). matches (RegEx) filelist. add (f); else {If (F. getname (). matches (RegEx) directorylist. add (f); search (f );}}}}
3. Test
Package mine. util; import Java. io. file; import Java. util. list; public class testsearchfile {public static void main (string [] ARGs) {system. out. println ("-------- specify all files and subdirectories in the directory -------"); List <File> List = (list <File>) New searchfile ("G: /Java/workspace/test/file "). files (); For (File file: List) system. out. println (file. getname (); system. out. println ("-------- specify a file with the suffix TXT in the directory ------"); List = (list <File>) New searchfile ("G:/Java/workspace/test/file ", ". *\\. TXT "). files (); For (File file: List) system. out. println (file. getname (); system. out. println ("-------- use this directory as the root directory of all files and subdirectories --"); List = (list <File>) New searchfile ("G: /Java/workspace/test "). allfilesanddirectory (); For (File file: List) system. out. println (file. getname ());}}
4. Results:
-------- Specify all files and subdirectories in the directory -------
AA. Data
BB. dat
Object
Test.txt
Test1.txt
Test2.txt
Test3.txt
Test4.txt
Test5
-------- Specify a file with the suffix TXT in the directory ------
Test.txt
Test1.txt
Test2.txt
Test3.txt
Test4.txt
-------- All files and subdirectories in the root directory --
. Classpath
. Project
. Settings
Org. Eclipse. jdt. Core. prefs
Bin
HTTP
Password. Class
Test. Class
Mine
Util
Searchfile. Class
Testsearchfile. Class
Test
A. Class
Arraysdemo. Class
B. Class
Bytearrayinputstreamdemo. Class
Datainputstreamandbytearrayinputstreamdemo. Class
Datainputstreamdemo. Class
Deepclonedemo. Class
Filedemo $1. Class
Filedemo. Class
Fileinputstreamdemo. Class