Sort lists the specified files/folders under the specified directory--"Thinking in Java" essay 027

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.