<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 鹽城小土包 閱讀(118) 評論(0)  編輯  收藏 所屬分類: J2EE

    <2025年5月>
    27282930123
    45678910
    11121314151617
    18192021222324
    25262728293031
    1234567

    導航

    統計

    常用鏈接

    留言簿

    隨筆檔案(14)

    文章分類(18)

    文章檔案(18)

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 亚洲免费视频在线观看| 久久久久亚洲精品中文字幕| 777亚洲精品乱码久久久久久 | 国产自产拍精品视频免费看| 亚洲AV无码无限在线观看不卡 | 精品亚洲成AV人在线观看| 中文字幕日本人妻久久久免费| 久久夜色精品国产亚洲av| 免费一级毛suv好看的国产网站| 免费一级毛片正在播放| 日韩亚洲综合精品国产| 亚洲色偷拍区另类无码专区| 51午夜精品免费视频| 久久夜色精品国产亚洲| 99久久久国产精品免费牛牛四川 | 久久久久亚洲AV成人无码| 一级毛片aaaaaa免费看| 亚洲免费观看网站| 蜜桃精品免费久久久久影院| 男男gay做爽爽的视频免费| 亚洲精品99久久久久中文字幕| 亚洲免费无码在线| 亚洲制服中文字幕第一区| 黄色免费网站网址| 亚洲1区2区3区精华液| 亚洲天堂免费在线视频| 国产精品免费福利久久| 亚洲伦理中文字幕| 国产91久久久久久久免费| 中文在线日本免费永久18近| 亚洲免费精彩视频在线观看| 最新猫咪www免费人成| xxxxx做受大片视频免费| 久久青青草原亚洲AV无码麻豆| 青青视频观看免费99| 亚洲1区2区3区精华液| 亚洲高清国产拍精品26U| 无码国产精品久久一区免费| 一个人看的免费视频www在线高清动漫| 久久精品国产精品亚洲精品| 人妻视频一区二区三区免费|