Java implementation of Windows dir command

Source: Internet
Author: User
Tags array sort dateformat sort

In order to make a file management system to achieve a very rough small example, the feeling is still worth studying. The following is the source code:

import Java.util.Comparator;


import Java.io.File;


     /**


* Created by IntelliJ idea.<br>


* <B>USER</B>: leizhimin<br>


* <B>DATE</B>: 2008-7-18 22:43:44<br>


* <b>note</b&gt: File sort, sorted by first directory after file way


     */


public class Filecomparator implements comparator<file> {


public int Compare (file O1, file O2) {


if (o1.isdirectory () && o2.isdirectory ()) return O1.compareto (O2);


Else if (o1.isdirectory () &&!o2.isdirectory ()) return-1;


Else if (!o1.isdirectory () && o2.isdirectory ()) return 1;


else return O1.compareto (O2);


       }


     }


Import Java.io.File;


import java.util.*;


import java.sql.Date;


import Java.text.DateFormat;


import Java.text.SimpleDateFormat;


import Java.text.NumberFormat;


     /**


* Created by IntelliJ idea.<br>


* <B>USER</B>: leizhimin<br>


* <B>DATE</B>: 2008-7-18 22:02:30<br>


* <b>note</b&gt: A routine that simulates the Dir test


     */


public class Testfile {


public static DateFormat df = new SimpleDateFormat ("Yyyy-mm-dd hh:mm");


int nl = 18;


public static void Main (String args[]) {


test ();


       }


public static void Test () {


System.out.println ("----------Test ()----------");


File File = new file ("F:\\soft_dev");


//File.isdirectory ();


//System.out.println (File.pathseparator);


//System.out.println (File.pathseparatorchar);


//System.out.println (File.separator);


//System.out.println (File.separatorchar);


Filecomparator fc = new Filecomparator ();


file[] lf = File.listfiles ();


Arrays.sort (LF, FC); Array sort


for (File f:lf) {


String datastr = Df.format (New Date (F.lastmodified ());


if (f.isdirectory ()) {


System.out.print (datastr + "<DIR>");


} else {


System.out.print (Datastr + "" + getformatstring (string.valueof (Numberformat.getinstance (). Format (F.length ())));


           }


System.out.println ("" + F.getname ());


         }


   }


//Format function


public static string getformatstring (string s) {


int nb = 12-s.length ();


StringBuilder sb = new StringBuilder ();


if (nb > 0) {


for (int n = 1; n <= nb; n++) {


sb = Sb.append ("");


           }


         }


return Sb.append (s). ToString ();


       }


     }

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.