• java IO复习(三)


    package com.zyw.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;
    
    /**
     * 目录实用工具
     * from think in java
     */
    public class Directory {
        public static File[] local(File dir,final String regex){
          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){
            return local(new File(path),regex);
        }
        public static class TreeInfo implements Iterable<File>{
            public List<File> files=new ArrayList<File>();
            public List<File> dirs=new ArrayList<File>();
            @Override
            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)+"
    
    files: "+PPrint.pformat(files);
            }
        }
        public static TreeInfo walk(String start,String regex){return recurseDirs(new File(start),regex);}
        public static TreeInfo walk(File start,String regex){return recurseDirs(start,regex);}
        public static TreeInfo walk(File start){return recurseDirs(start,".");}
        public static TreeInfo walk(String start){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 {
                    if(item.getName().matches(regex))
                        result.files.add(item);
                }
            }
            return result;
        }
        public  static void main(String args[]){
            System.out.println(walk("G:\zyw",".*\.class"));
        }
    }
    package com.zyw.io;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    /**
     * 打印工具
     */
    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("
     ");
                result.append(elem);
            }
            if(c.size()!=1) result.append("
    ");
                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)));
        }
    }
    
    
    
    
    
  • 相关阅读:
    DataTables合并单元格(rowspan)的实现思路(多分组分类的情况)
    DataTables固定表格宽度(设置横向滚动条)
    用图片替代cursor光标样式
    DataTables获取指定元素的行数据
    任意表格(table)实现拖动列(column)改变列大小
    鼠标拖动改变DIV等网页元素的大小的最佳实践
    DataTables实现rowspan思路
    DataTables添加额外的查询参数和删除columns等无用参数
    击穿(强推---神仙代码)
    java jvm 参数 -Xms -Xmx -Xmn -Xss 调优总结
  • 原文地址:https://www.cnblogs.com/yunwuzhan/p/5312086.html
Copyright © 2020-2023  润新知