Java教程:Java實現(xiàn)Windows的dir命令

字號:

為做一個文件管理系統(tǒng)所實現(xiàn)的一個很粗糙的小例子,感覺還是有研究價值的。 下面是源代碼:  import java.util.Comparator;
    import java.io.File;
    /**
    * Created by IntelliJ IDEA.
    * User: leizhimin
    * Date: 2008-7-18 22:43:44
    * Note: 文件排序,按照先目錄后文件方式排
    */
    public class FileComparator implements Comparator {
    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.
    * User: leizhimin
    * Date: 2008-7-18 22:02:30
    * Note: 模擬dir測試的例程
    */
    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);  //數(shù)組排序
    for (File f : lf) {
    String dataStr = df.format(new Date(f.lastModified()));
    if (f.isDirectory()) {
    System.out.print(dataStr + "     ");
    } else {
    System.out.print(dataStr + " " + getFormatString(String.valueOf(NumberFormat.getInstance().format(f.length()))));
    }
    System.out.println(" " + f.getName());
    }
    }
    //格式化函數(shù)
    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();
    }
    }
    運行效果:
    ----------test()----------
    2008-06-20 13:01  ACDSee v3.1 SR1 美化版 Plus
    2008-06-20 13:01  MyEclipse Enterprise Workbench 6.0.1 GA
    2008-06-20 13:02  PLSQL Developer v7.1.4 英文版
    2008-06-20 13:02  SQLyog Enterprise v6.5 英文版
    2008-07-02 18:04  staruml-5.0
    2008-07-07 18:45  Stylus Studio 2008 XML Enterprise Suite v9.1.1050g 官方中文企業(yè)版
    2008-06-20 13:02  Sybase.PowerDesigner.v12.5.0.2169
    2008-06-20 13:02  輸入法設(shè)置工具 IME TOOL v2.5.9
    2008-04-10 18:56  23,510,720 dotnetfx.exe
    2006-06-15 11:22  54,666,120 idea-5.1.2.exe
    2008-03-18 19:16  90,648,728 idea-7.0.3.exe
    2008-04-11 16:08    428,538 IEDevToolBarSetup.rar
    2006-06-18 00:00    148,480 IntJ512kg.exe
    2008-01-31 11:12  54,278,424 jdk-1_5_0_14-windows-i586-p.exe
    2008-05-29 19:07  184,893,774 MyEclipse_6.0.1GA_E3.3.1_Installer.exe
    2008-06-24 11:33  461,013,238 MyEclipse_6.5.0GA_E3.3.2_Installer.exe
    2008-05-30 08:15   4,320,768 mysql-connector-odbc-5.1.4-win32.msi
    2007-02-04 11:08  541,075,612 Rational Rose Enterprise V7.0.nrg
    2001-05-26 21:3123,264 rational_perm.dat
    2008-04-14 10:39  10,000,152 spket-1.6.11.jar
    2008-06-27 14:45    113,784 tcpTrace081.zip
    2008-05-12 15:31  28,847,842 ToadForMySQLFreeware_3.1.1.462.zip
    2008-04-12 09:57   1,833,339 UltraISO 9.12 簡體中文版.rar
    2008-06-11 14:39  16,672,737 Windows Live Messenger(MSN) 8.5.1302.1018_簡體去廣告搜索多開版_微軟聊天工具.rar