熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> JSP教程 >> 正文

Java實現Windows的dir命令

2022-06-13   來源: JSP教程 

  為做一個文件管理系統所實現的一個很粗糙的小例子感覺還是有研究價值的

  下面是源代碼

  import javautilComparator;
        import javaioFile;
        /**
        * Created by IntelliJ IDEA<br>
        * <b>User</b>: leizhimin<br>
        * <b>Date</b>: ::<br>
        * <b>Note</b>: 文件排序按照先目錄後文件方式排
        */
        public class FileComparator implements Comparator<File> {

  public int compare(File o File o) {
                if (oisDirectory() && oisDirectory()) return pareTo(o);
                else if(oisDirectory() && !oisDirectory()) return ;
                else if(!oisDirectory() && oisDirectory()) return ;
                else return pareTo(o);
            }
        }

  

  import javaioFile;
        import javautil*;
        import javasqlDate;
        import javatextDateFormat;
        import javatextSimpleDateFormat;
        import javatextNumberFormat;
        /**
        * Created by IntelliJ IDEA<br>
        * <b>User</b>: leizhimin<br>
        * <b>Date</b>: ::<br>
        * <b>Note</b>: 模擬dir測試的例程
        */
        public class TestFile {
            public static DateFormat df = new SimpleDateFormat(yyyyMMdd HH:mm);
            int nl = ;
            public static void main(String args[]) {
                test();
            }
            public static void test() {
                Systemoutprintln(test());
                File file = new File(F:\\SOFT_DEV);
        //        fileisDirectory();
        //        Systemoutprintln(FilepathSeparator);
        //        Systemoutprintln(FilepathSeparatorChar);
        //        Systemoutprintln(Fileseparator);
        //        Systemoutprintln(FileseparatorChar);
                FileComparator fc = new FileComparator();
                File[] lf = filelistFiles();
                Arrayssort(lf fc);    //數組排序
                for (File f : lf) {
                    String dataStr = dfformat(new Date(flastModified()));

  if (fisDirectory()) {
                        Systemoutprint(dataStr +   <DIR>       );

  } else {
                        Systemoutprint(dataStr +   + getFormatString(StringvalueOf(NumberFormatgetInstance()format(flength()))));
                    }
                    Systemoutprintln(  + fgetName());
                }

  }
            //格式化函數
            public static String getFormatString(String s) {
                int nb = slength();
                StringBuilder sb = new StringBuilder();
                if (nb > ) {
                    for (int n = ; n <= nb; n++) {
                        sb = sbappend( );
                    }
                }
                return sbappend(s)toString();
            }
        }

  運行效果

            test()
        :  <DIR>         ACDSee v SR 美化版 Plus
        :  <DIR>         MyEclipse Enterprise Workbench GA
        :  <DIR>         PLSQL Developer v 英文版
        :  <DIR>         SQLyog Enterprise v 英文版
        :  <DIR>         staruml
        :  <DIR>         Stylus Studio XML Enterprise Suite vg 官方中文企業版
        :  <DIR>         SybasePowerDesignerv
        :  <DIR>         輸入法設置工具 IME TOOL v
        :      dotnetfxexe
        :      ideaexe
        :      ideaexe
        :         IEDevToolBarSetuprar
        :         IntJkgexe
        :      jdk___windowsipexe
        :     MyEclipse_GA_E_Installerexe
        :     MyEclipse_GA_E_Installerexe
        :       mysqlconnectorodbcwinmsi
        :     Rational Rose Enterprise Vnrg
        :          rational_permdat
        :      spketjar
        :         tcpTracezip
        :      ToadForMySQLFreeware_zip
        :       UltraISO 簡體中文版rar
        :      Windows Live Messenger(MSN) _簡體去廣告搜索多開版_微軟聊天工具rar

  blog的在線編輯器顯示好像有點問題貼個圖吧



 


From:http://tw.wingwit.com/Article/program/Java/JSP/201311/19733.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.