• java 编程思想 Directory工具类


      1 package net.bigwrok;
      2 
      3 import java.io.File;
      4 import java.io.FilenameFilter;
      5 import java.io.IOException;
      6 import java.util.ArrayList;
      7 import java.util.Iterator;
      8 import java.util.List;
      9 import java.util.regex.Pattern;
     10 
     11 public class Directory_util {
     12     /*工具类 遍历当前目录的所有文件,可以查看所有 目录 ,以及文件
     13     
     14     */
     15     public static File[] local(File dir,final String reg){
     16         //遍历当前目录的文件
     17         return dir.listFiles(new FilenameFilter() {
     18             //文件名过滤器
     19             private Pattern p = Pattern.compile(reg);
     20             @Override
     21             public boolean accept(File dir, String name) {
     22                  System.out.println(dir+"  "+name);
     23                 return p.matcher(name).matches();
     24                 //ret name.endsWith("java");
     25             }
     26         });//return 
     27     }//local
     28 
     29         
     30     //遍历当前目录的文件
     31     public static File[] local(String path,final String reg){
     32         return local(new File(path),reg);
     33     }
     34     //内部类  files保存 所有文件的  dirs保存所有的目录
     35     public static class TreeInfo implements Iterable<File>{
     36         public List<File> files  =  new ArrayList<File>();
     37         public List<File> dirs  =  new ArrayList<File>();
     38         @Override
     39         public Iterator<File> iterator() {
     40             return files.iterator();
     41         }
     42         
     43         void addAll(TreeInfo other){
     44             files.addAll(other.files);
     45             dirs.addAll(other.dirs);
     46         }
     47         
     48         @Override
     49         public String toString() {
     50             // 格式化打印
     51             //return "dirs: " +dirs+"
    
    files: "+files;
     52             return "dirs: " +PPrint.pformat(dirs)+"
    
    files: "+PPrint.pformat(files);
     53         }
     54         
     55     }//静态内部类class TreeInfo  结束
     56     
     57     static TreeInfo recurseDirs(File startDir ,String reg){
     58         TreeInfo result = new TreeInfo();
     59         for (File item : startDir.listFiles()) {
     60             if (item.isDirectory()) {
     61                 //如果是目录,目录列表dirs增加 item,目录下面所有的文件都放进result
     62                 result.dirs.add(item);
     63                 result.addAll(recurseDirs(item,reg));
     64             }else {
     65                 if (startDir.getName().matches(reg)) {
     66                     // getName()获得文件名称,仅仅是文件名 不包括目录  如:xxxx.java
     67                     result.files.add(item);
     68                 }
     69             }
     70         }//for
     71         return result;
     72         
     73     }//recurseDirs 方法结束
     74    
        // walk 从当前目录一直遍历所有文件 75 public static TreeInfo walk(String start ,String reg){ 76 return recurseDirs(new File(start), reg); 77 } 78 79 80 public static TreeInfo walk(File start ,String reg){ 81 return recurseDirs( start , reg); 82 } 83 84 85 public static TreeInfo walk(String start ){ 86 return recurseDirs(new File(start), ".*"); 87 } 88 89 90 public static TreeInfo walk(File start ){ 91 return recurseDirs( start , ".*"); 92 } 93 94 95 96 public static void main(String[] args) throws IOException { 97 File test = new File("."); 98 System.out.println("绝对路径 "+test.getAbsolutePath()); 99 System.out.println("抽象路径 "+test.getCanonicalPath()); 100 TreeInfo str = walk( "D:\dev2\workspace\seehope\bigwork\src\com\"); 101 System.out.println(str); 102 /* 103 files: [D:dev2workspaceseehopeigworksrccomColorChoseListener.java, 104 D:dev2workspaceseehopeigworksrccomReadImg.java, 105 D:dev2workspaceseehopeigworksrccomTrueFrametest.java] 106 */ 107 108 TreeInfo str2 = walk( test); 109 System.out.println(str2); 110 111 /* 112 dirs: [..settings, .in, .inc2, .incom, .in et, .in etigwrok, .src, .srcc2, .srccom, .src et, .src etigwrok] 113 114 files: [..classpath, ..project, ..settingsorg.eclipse.jdt.core.prefs, .inc2Draw2.class, .inc2Drawing$1.class, .inc2Drawing.class, .inc2f.jpg, .inc2MymenuAdapter.class, .inc2MyMenuItemAction.class, .inc2MyStrokeAction.class, .inc2ShapeAndColor.class, .inc2TrueFrametest$1.class, .inc2TrueFrametest$2.class, .inc2TrueFrametest$3.class, .inc2TrueFrametest.class, .incomColorChoseListener.class, .incomDraw2.class, .incomMymenuAdapter.class, .incomMyMenuItemAction.class, .incomMyStrokeAction.class, .incomReadImg.class, .incomShapeAndColor.class, .incomTrueFrametest$1.class, .incomTrueFrametest$2.class, .incomTrueFrametest$3.class, .incomTrueFrametest$4.class, .incomTrueFrametest.class, .in etigwrokDirectory_util$1.class, .in etigwrokDirectory_util$TreeInfo.class, .in etigwrokDirectory_util.class, .in etigwrokInetAddrTest.class, .in etigwrokInetAddrTest2.class, .in etigwrokIO1.class, .in etigwrokTest2_net.class, .in etigwrokTest_net2.class, .srcc2Drawing.java, .srcc2f.jpg, .srcc2TrueFrametest.java, .srccomColorChoseListener.java, .srccomReadImg.java, .srccomTrueFrametest.java, .src etigwrokDirectory_util.java, .src etigwrokInetAddrTest.java, .src etigwrokInetAddrTest2.java, .src etigwrokIO1.java, .src etigwrokTest2_net.java, .src etigwrokTest_net2.java] 115 116 */ 117 /* 118 for (File file : Directory_util.walk(".", ".*[Zz].*\.java")) { 119 System.out.println(file); 120 } 121 walk(start,reg); 122 ".*[Zz].*\.java" 文件以.java结尾 123 "T.*" T开头的任何文件 124 "T.*\.java" T开头的.java文件 125 */ 126 } 127 128 129 130 }

    打印工具类PPrint

     1 package net.bigwrok;
     2 
     3 import java.util.Arrays;
     4 import java.util.Collection;
     5 
     6 public class PPrint {
     7     
     8     
     9     
    10     public static String pformat(Collection<?> c){ 
    11         if (c.size() == 0) {
    12             return "[]";
    13         }
    14         
    15         StringBuilder res = new StringBuilder("[");
    16         for (Object elem : c) {
    17             if (c.size() != 1) {
    18                 res.append("
    ");
    19             }
    20             res.append(elem);
    21         }//for
    22 
    23         if (c.size() != 1) {
    24             res.append("
    ");
    25         }
    26         res.append("]");
    27         return res.toString();
    28     }//pformat
    29     
    30     
    31     public static void pprint(Collection<?> c){
    32         System.out.println(pformat(c));
    33     }
    34 
    35     
    36     public static void pprint(Object[] c){
    37         System.out.println(Arrays.asList(c));
    38     }
    39     
    40     
    41     
    42     
    43 
    44 }

    运行结果

    files: [
    ..classpath
    ..project
    ..settingsorg.eclipse.jdt.core.prefs
    .inc2Draw2.class
    .inc2Drawing$1.class
    .inc2Drawing.class
    .inc2f.jpg
    .inc2MymenuAdapter.class
    .inc2MyMenuItemAction.class
    .inc2MyStrokeAction.class
    .inc2ShapeAndColor.class
    .inc2TrueFrametest$1.class
    .inc2TrueFrametest$2.class
    .inc2TrueFrametest$3.class
    .inc2TrueFrametest.class
    .incomColorChoseListener.class
    .incomDraw2.class
    .incomMymenuAdapter.class
    .incomMyMenuItemAction.class
    .incomMyStrokeAction.class
    .incomReadImg.class
    .incomShapeAndColor.class
    .incomTrueFrametest$1.class
    .incomTrueFrametest$2.class
    .incomTrueFrametest$3.class
    .incomTrueFrametest$4.class
    .incomTrueFrametest.class
    .in
    etigwrokDirectory_util$1.class
    .in
    etigwrokDirectory_util$TreeInfo.class
    .in
    etigwrokDirectory_util.class
    .in
    etigwrokInetAddrTest.class
    .in
    etigwrokInetAddrTest2.class
    .in
    etigwrokIO1.class
    .in
    etigwrokIO2.class
    .in
    etigwrokPPrint.class
    .in
    etigwrokTest2_net.class
    .in
    etigwrokTest_net2.class
    .srcc2Drawing.java
    .srcc2f.jpg
    .srcc2TrueFrametest.java
    .srccomColorChoseListener.java
    .srccomReadImg.java
    .srccomTrueFrametest.java
    .src
    etigwrokDirectory_util.java
    .src
    etigwrokInetAddrTest.java
    .src
    etigwrokInetAddrTest2.java
    .src
    etigwrokIO1.java
    .src
    etigwrokIO2.java
    .src
    etigwrokPPrint.java
    .src
    etigwrokTest2_net.java
    .src
    etigwrokTest_net2.java
    ]

  • 相关阅读:
    图片中添加文字
    几种经典的滤波算法(转)
    OPENCV初试
    图像处理和图像识别中常用的OpenCV函数
    SIP开发
    【转】opencv老是卡在某一层,
    大电子文件读取成二进制流方案
    C# 调试方法之即时窗口输出
    关于如何解锁你的WP7,以便安装自己开发的程序。
    Windows phone 7 之初体验(一.安装Windows phone 7 sdk)
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9126897.html
Copyright © 2020-2023  润新知