Java实现Windows的dir命令



 



为做一个文件管理系统所实现的一个很粗糙的小例子,感觉还是有研究价值的。



 



下面是源代码:



 



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>: 文件排序,按照先目录后文件方式排 
*/ 
    
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>: 模拟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);     
    //数组排序  
    
         
    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()); 
    
        } 
    

    } 
    

     
    //格式化函数  
    
     
    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  <DIR>         ACDSee v3.1 SR1 美化版 Plus
2008-06-20 13:01  <DIR>         MyEclipse Enterprise Workbench 6.0.1 GA
2008-06-20 13:02  <DIR>         PLSQL Developer v7.1.4 英文版
2008-06-20 13:02  <DIR>         SQLyog Enterprise v6.5 英文版
2008-07-02 18:04  <DIR>         staruml-5.0
2008-07-07 18:45  <DIR>         Stylus Studio 2008 XML Enterprise Suite v9.1.1050g 官方中文企业版
2008-06-20 13:02  <DIR>         Sybase.PowerDesigner.v12.5.0.2169
2008-06-20 13:02  <DIR>         输入法设置工具 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:31        23,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