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>: 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>: 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 ();
}
}