Directory untilities

          A common task in programming is to perform operations on sets of files, either in the local directory or by walking the entire durectory tree.
          It is useful to have a tool that will produce the set of files for you.
          package think.in.java.io;

          import java.io.File;
          import java.io.FilenameFilter;
          import java.util.ArrayList;
          import java.util.Iterator;
          import java.util.List;
          import java.util.regex.Pattern;

          /**
           * Produce a seqence of File objects that match a regular expression in either a local directory,
           * or by walking a directory tree.
           * 
          @author WPeng
           *
           * 
          @since 2012-11-2
           
          */
          public final class Directory {
              
              
          public static File[] local(File dir, final String regex){
                  
          /** Returns an array of abstract pathnames denoting the files and directories in the directory 
                   * denoted by this abstract pathname that satisfy the specified filter. 
          */
                  
          return dir.listFiles(new FilenameFilter() {
                      
          private Pattern pattern = Pattern.compile(regex);
                      @Override
                      
          public boolean accept(File dir, String name) {
                          
          return pattern.matcher(new File(name).getName()).matches();
                      }
                  });
              }
              
              
          public static File[] local(String path, final String regex){
                  
          //Overloaded
                  return local(new File(path), regex);
              }
              
              
          // A two-tuple for returning a pair of objects
              public static class TreeInfo implements Iterable<File>{
                  
          public List<File> files = new ArrayList<File>();
                  
          public List<File> dirs = new ArrayList<File>();
                  
          // The default iterable element is the file list
                  public Iterator<File> iterator(){
                      
          return files.iterator();
                  }
                  
          void addAll(TreeInfo other){
                      files.addAll(other.files);
                      dirs.addAll(other.dirs);
                  }
                  
          public String toString(){
                      
          return "dirs: " + PPrint.pformat(dirs) + "\n\nfiles: " + PPrint.pformat(files);
                  }
              }
              
              
          public static TreeInfo walk(String start, String regex){
                  
          // Begin recurion
                  return recurseDirs(new File(start), regex);
              }
              
          public static TreeInfo walk(File start, String regex){
                  
          // Overload
                  return recurseDirs(start, regex);
              }
              
          public static TreeInfo walk(File start){
                  
          // Everything
                  return recurseDirs(start, ".*");
              }
              
          public static TreeInfo walk(String start){
                  
          // Overload
                  return recurseDirs(new File(start), ".*");
              }
              
          static TreeInfo recurseDirs(File startDir, String regex){
                  TreeInfo result 
          = new TreeInfo();
                  
          for(File item : startDir.listFiles()){
                      
          if (item.isDirectory()) {
                          result.dirs.add(item);
                          result.addAll(recurseDirs(item, regex));
                      }
          else// Regular file
                          if (item.getName().matches(regex)) {
                              result.files.add(item);
                          }
                      }
                  }
                  
          return result;
              }
              
              
          // Simple validation test
              public static void main(String[] args){
                  
          if(args.length == 0){
                      System.out.println(walk(
          "D:\\Users\\wpeng\\workspace\\Hello\\src\\think\\in\\java\\io"));
                  }
          else{
                      
          for(String arg : args){
                          System.out.println(arg);
                      }
                  }
              }
              
              
          }

          package think.in.java.io;

          import java.util.Arrays;
          import java.util.Collection;

          /**
           * Pretty-printer for collections
           * 
          @author WPeng
           *
           * 
          @since 2012-11-2
           
          */

          public class PPrint {
              
          public static String pformat(Collection<?> c) {
                  
          if(c.size() == 0){
                      
          return "[]";
                  }
                  StringBuilder result 
          = new StringBuilder("[");
                  
          for(Object elem : c){
                      
          if(c.size() != 1){
                          result.append(
          "\n ");
                      }
                      result.append(elem);
                  }
                  
          if(c.size() != 1){
                      result.append(
          "\n");
                  }
                  result.append(
          "]");
                  
          return result.toString();
              }
              
              
          public static void pprint(Collection<?> c){
                  System.out.println(pformat(c));
              }
              
              
          public static void pprint(Object[] c){
                  System.out.println(pformat(Arrays.asList(c)));
              }
          }

          /**
          dirs: [
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\1
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\2
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\3
          ]

          files: [
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\1.txt
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\2.txt
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\3.txt
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\Directory.java
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList.java
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList2.java
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\DirList3.java
           D:\Users\wpeng\workspace\Hello\src\think\in\java\io\PPrint.java
          ]
          */

          posted on 2012-11-02 14:19 鹽城小土包 閱讀(121) 評(píng)論(0)  編輯  收藏 所屬分類(lèi): J2EE

          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          導(dǎo)航

          統(tǒng)計(jì)

          常用鏈接

          留言簿

          隨筆檔案(14)

          文章分類(lèi)(18)

          文章檔案(18)

          搜索

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 苍梧县| 谷城县| 小金县| 思茅市| 永兴县| 额尔古纳市| 博湖县| 马鞍山市| 德兴市| 宣恩县| 定安县| 怀化市| 仙游县| 铁岭市| 曲周县| 黔南| 姚安县| 靖安县| 城市| 榕江县| 夏津县| 扎赉特旗| 德令哈市| 水城县| 珲春市| 时尚| 宜城市| 昌江| 太和县| 康乐县| 岳阳县| 澎湖县| 肇庆市| 子长县| 阳城县| 辽阳市| 石景山区| 扬州市| 江西省| 桃源县| 怀远县|