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+"\n\nfiles: "+files; 52 return "dirs: " +PPrint.pformat(dirs)+"\n\nfiles: "+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:\dev2\workspace\seehope\bigwork\src\com\ColorChoseListener.java, 104 D:\dev2\workspace\seehope\bigwork\src\com\ReadImg.java, 105 D:\dev2\workspace\seehope\bigwork\src\com\TrueFrametest.java] 106 */ 107 108 TreeInfo str2 = walk( test); 109 System.out.println(str2); 110 111 /* 112 dirs: [.\.settings, .\bin, .\bin\c2, .\bin\com, .\bin\net, .\bin\net\bigwrok, .\src, .\src\c2, .\src\com, .\src\net, .\src\net\bigwrok] 113 114 files: [.\.classpath, .\.project, .\.settings\org.eclipse.jdt.core.prefs, .\bin\c2\Draw2.class, .\bin\c2\Drawing$1.class, .\bin\c2\Drawing.class, .\bin\c2\f.jpg, .\bin\c2\MymenuAdapter.class, .\bin\c2\MyMenuItemAction.class, .\bin\c2\MyStrokeAction.class, .\bin\c2\ShapeAndColor.class, .\bin\c2\TrueFrametest$1.class, .\bin\c2\TrueFrametest$2.class, .\bin\c2\TrueFrametest$3.class, .\bin\c2\TrueFrametest.class, .\bin\com\ColorChoseListener.class, .\bin\com\Draw2.class, .\bin\com\MymenuAdapter.class, .\bin\com\MyMenuItemAction.class, .\bin\com\MyStrokeAction.class, .\bin\com\ReadImg.class, .\bin\com\ShapeAndColor.class, .\bin\com\TrueFrametest$1.class, .\bin\com\TrueFrametest$2.class, .\bin\com\TrueFrametest$3.class, .\bin\com\TrueFrametest$4.class, .\bin\com\TrueFrametest.class, .\bin\net\bigwrok\Directory_util$1.class, .\bin\net\bigwrok\Directory_util$TreeInfo.class, .\bin\net\bigwrok\Directory_util.class, .\bin\net\bigwrok\InetAddrTest.class, .\bin\net\bigwrok\InetAddrTest2.class, .\bin\net\bigwrok\IO1.class, .\bin\net\bigwrok\Test2_net.class, .\bin\net\bigwrok\Test_net2.class, .\src\c2\Drawing.java, .\src\c2\f.jpg, .\src\c2\TrueFrametest.java, .\src\com\ColorChoseListener.java, .\src\com\ReadImg.java, .\src\com\TrueFrametest.java, .\src\net\bigwrok\Directory_util.java, .\src\net\bigwrok\InetAddrTest.java, .\src\net\bigwrok\InetAddrTest2.java, .\src\net\bigwrok\IO1.java, .\src\net\bigwrok\Test2_net.java, .\src\net\bigwrok\Test_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("\n"); 19 } 20 res.append(elem); 21 }//for 22 23 if (c.size() != 1) { 24 res.append("\n"); 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 .\.settings\org.eclipse.jdt.core.prefs .\bin\c2\Draw2.class .\bin\c2\Drawing$1.class .\bin\c2\Drawing.class .\bin\c2\f.jpg .\bin\c2\MymenuAdapter.class .\bin\c2\MyMenuItemAction.class .\bin\c2\MyStrokeAction.class .\bin\c2\ShapeAndColor.class .\bin\c2\TrueFrametest$1.class .\bin\c2\TrueFrametest$2.class .\bin\c2\TrueFrametest$3.class .\bin\c2\TrueFrametest.class .\bin\com\ColorChoseListener.class .\bin\com\Draw2.class .\bin\com\MymenuAdapter.class .\bin\com\MyMenuItemAction.class .\bin\com\MyStrokeAction.class .\bin\com\ReadImg.class .\bin\com\ShapeAndColor.class .\bin\com\TrueFrametest$1.class .\bin\com\TrueFrametest$2.class .\bin\com\TrueFrametest$3.class .\bin\com\TrueFrametest$4.class .\bin\com\TrueFrametest.class .\bin\net\bigwrok\Directory_util$1.class .\bin\net\bigwrok\Directory_util$TreeInfo.class .\bin\net\bigwrok\Directory_util.class .\bin\net\bigwrok\InetAddrTest.class .\bin\net\bigwrok\InetAddrTest2.class .\bin\net\bigwrok\IO1.class .\bin\net\bigwrok\IO2.class .\bin\net\bigwrok\PPrint.class .\bin\net\bigwrok\Test2_net.class .\bin\net\bigwrok\Test_net2.class .\src\c2\Drawing.java .\src\c2\f.jpg .\src\c2\TrueFrametest.java .\src\com\ColorChoseListener.java .\src\com\ReadImg.java .\src\com\TrueFrametest.java .\src\net\bigwrok\Directory_util.java .\src\net\bigwrok\InetAddrTest.java .\src\net\bigwrok\InetAddrTest2.java .\src\net\bigwrok\IO1.java .\src\net\bigwrok\IO2.java .\src\net\bigwrok\PPrint.java .\src\net\bigwrok\Test2_net.java .\src\net\bigwrok\Test_net2.java ]
posted on 2018-06-02 20:56 wait_for_you 阅读(847) 评论(0) 编辑 收藏 举报