1 //: Sorteddirlist.java2 ///This program can list a directory. 3 4 PackageC10;5 6 ImportJava.io.File;7 ImportJava.io.FilenameFilter;8 Importjava.util.Arrays;9 ImportJava.util.Comparator;Ten One /** A * @time: Morning 11:52:04 - * @date: April 29, 2017 - * @auther: Skyfffire the * @version: v0.1 - * - * Sortable read-in file directory, value sort=-1 to descending order - * Value sort=1 for traditional ascending (according to ASCII code) + */ - Public classSorteddirlist { + PrivateFile Path =NULL; A Privatestring[] List =NULL; at Private intSort = 1; - - /** - * - * @paramFilespath the path to read - * @paramAFN filter characters, if NULL then do not filter in * @paramsort sorted by,-1 descending, 1 ascending - */ to PublicSorteddirlist (FinalString Filespath, + FinalString AFN,intsort) { - This. Sort =sort; thePath =NewFile (filespath); * $ if(AFN = =NULL) {Panax NotoginsengList =path.list (); -}Else { theList = Path.list (NewFilenameFilter () { + @Override A Public BooleanAccept (File dir, String name) { theString n =NewFile (name). GetName (); + - returnN.indexof (AFN)! =-1; $ } $ }); - } - the /** - * Manual implementation of the sort method of the internal sorting mechanism, so that can be dynamically ascending/descending sortWuyi */ theArrays.sort (list,NewComparator<string>() { - @Override Wu Public intCompare (String O1, String O2) { - returnSort *(O1.compareto (O2)); About } $ }); - } - - /** A * for printing list + */ the voidprint () { - for(String nowpath:list) { $ System.out.println (nowpath); the } the } the the /** - * @returnThe sort in */ the Public intGetsort () { the returnsort; About } the the /** the * @paramsort the sort to set + */ - Public voidSetsort (intsort) { the This. Sort =sort;Bayi } the the Public Static voidMain (string[] args) { - //Do not filter -Sorteddirlist SDL =NewSorteddirlist ("c:\\",NULL, 1); the the sdl.print (); the } the } - the ///:~
I use my own C packing directory as an example, the result of ascending sort is:
$Recycle. Bin
Bootnxt
Documents and Settings
Onedrivetemp
PerfLogs
Program Files
Program Files (x86)
ProgramData
Qmdownload
Recovery
Symcache
System Volume Information
Users
Windows
Bootmgr
Pagefile.sys
Swapfile.sys
The result of descending sort is:
Swapfile.sys
Pagefile.sys
Bootmgr
Windows
Users
System Volume Information
Symcache
Recovery
Qmdownload
ProgramData
Program Files (x86)
Program Files
PerfLogs
Onedrivetemp
Documents and Settings
Bootnxt
$Recycle. Bin
You can change the sorting method quickly by changing only one sign.
Sort lists the specified files/folders under the specified directory--"Thinking in Java" essay 027