zoukankan      html  css  js  c++  java
  • 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
    ]

  • 相关阅读:
    今天刚开通博客,很开心
    ZYNQ双核AMP开发详解(USE_AMP -DUSE_AMP=1 含义和作用详解)
    赛灵思机器学习套件(ML Suite) v1.4 DPU, DNNK
    petalinux add pre-build application to rootfs compile faliure solution
    如何用SDK生成device tree
    Zynq UltraScale+ MPSoC device tree 中 CCF中 clock output数字映射表
    Zynq ZC702平台 QSPI + eMMC实现
    Zynq ZC702平台 Linux + Baremetal AMP实现(一)【快速启动CPU1】
    第五章 任务执行
    第四章 闭锁
  • 原文地址:https://www.cnblogs.com/kwaitfort/p/9126897.html
Copyright © 2011-2022 走看看