俊星的BLOG

          我的JAVA工具之目錄結構輸出

          1、代碼:
          package test;

          import java.io.File;

          public class DMain {
              
          static int level = 0;

              
          /** 文件目錄迭代輸出 */
              
          static void pathPrint1(String root) {
                  File path 
          = new File(root);
                  
          if (path.isDirectory()) {
                      System.out.println(getMultiStr(
          " ", level) + (path.isDirectory() ? "<dir>" : ""+ path.getName());
                      level
          ++;
                      File[] files 
          = path.listFiles();
                      
          for (File f : files) {
                          
          if (f.isDirectory()) {
                              pathPrint1(f.getAbsolutePath());
                          } 
          else {
                              System.out.println(getMultiStr(
          " ", level) + f.getName());
                          }
                      }
                      level
          --;
                  }
              }

              
          static String getMultiStr(String str, int num) {
                  String s 
          = "";
                  
          for (int i = 0; i < num; i++) {
                      s 
          += str;
                  }
                  
          return s;
              }

              
          /** 以樹形結構輸出目錄 */
              
          static void printTree(String root, String init) {
                  File path 
          = new File(root);
                  
          if (path.isDirectory()) {
                      File[] files 
          = path.listFiles();
                      
          for (int i = 0, length = files.length; i < length; i++) {
                          File f 
          = files[i];
                          System.out.println(init 
          + "|" + getMultiStr("-"4+ f.getName());
                          
          if (f.isDirectory()) {
                              printTree(f.getAbsolutePath(), init 
          + (i == length - 1 ? " " : "|"+ getMultiStr(" "4));
                          }
                      }
                  }
              }

              
          public static void main(String[] args) {
                  String s 
          = "D:\\My Documents\\discuz!\\Discuz_7[1].0.0_SC_GBK\\upload\\forumdata";
                  pathPrint1(s);
                  System.out.println(
          "------------------------");
                  System.out.println(
          "ROOT");
                  printTree(s,
          "");

              }

          }

          2、得到的輸出:
          <dir>forumdata
           <dir>cache
            index.htm
           index.htm
           <dir>logs
            index.htm
           <dir>templates
            index.htm
           <dir>threadcaches
            index.htm
          ------------------------
          ROOT
          |----cache
          |    |----index.htm
          |----index.htm
          |----logs
          |    |----index.htm
          |----templates
          |    |----index.htm
          |----threadcaches
               |----index.htm


          posted on 2009-05-18 20:18 俊星 閱讀(198) 評論(0)  編輯  收藏


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 石门县| 正定县| 大宁县| 桓仁| 建水县| 兴国县| 延吉市| 柞水县| 鹤峰县| 江都市| 宜都市| 庆城县| 河北区| 宿松县| 凌源市| 竹溪县| 浦东新区| 蒙城县| 河源市| 博爱县| 静乐县| 涞水县| 广宗县| 札达县| 隆回县| 宜兴市| 乳源| 商河县| 介休市| 夹江县| 鞍山市| 区。| 苏州市| 灵川县| 临夏县| 阳泉市| 平和县| 监利县| 井冈山市| 江孜县| 车险|