List泛型自定义类型排序和大小比较 java版

今天无聊,把重复文件删除工具在java上实现了一下

明显java下的工作量上去了,Directory居然都没有,汗,还有文件名模糊查找.....自己写或者网上抄吧

 /**
     * JAVA的文件名模糊查找
     *
     * @param pattern 通配符模式
     * @param str 待匹配的字符串
     * @return 匹配成功则返回true,否则返回false
     */
    private static boolean FileMatch(String pattern, String str) {
        int patternLength = pattern.length();
        int strLength = str.length();
        int strIndex = 0;
        char ch;
        for (int patternIndex = 0; patternIndex < patternLength; patternIndex++) {
            ch = pattern.charAt(patternIndex);
            if (ch == '*') {
                //通配符星号*表示可以匹配任意多个字符
                while (strIndex < strLength) {
                    if (FileMatch(pattern.substring(patternIndex + 1),
                            str.substring(strIndex))) {
                        return true;
                    }
                    strIndex++;
                }
            } else if (ch == '?') {
                //通配符问号?表示匹配任意一个字符
                strIndex++;
                if (strIndex > strLength) {
                    //表示str中已经没有字符匹配?了。
                    return false;
                }
            } else {
                if ((strIndex >= strLength) || (ch != str.charAt(strIndex))) {
                    return false;
                }
                strIndex++;
            }
        }
        return (strIndex == strLength);
    }


public class Directory {

    private String path;

    /**
     * 构造函数
     *
     * @param dirpath
     */
    public Directory(String dirpath) {
        if (dirpath == null || dirpath.length() < 1) {
            return;
        }
        this.path = dirpath;
    }
/**
     * 获得给定路径下所有符合条件的文件
     *
     * @param filePattern
     * @return
     */
    public List getAllFiles(String filePattern) {
        if (tmpPath == null) {
            tmpPath = path;
        }
        File file = new File(tmpPath);
        File[] files = file.listFiles();
        for (int i = 0; i < files.length; i++) {
            if (files[i].exists()) {
                if (files[i].isFile()) {
                    if (FileMatch(filePattern, files[i].getName())) {
                        fileList.add(files[i]);
                    }
                } else if (files[i].isDirectory()) {
                    tmpPath = files[i].getPath();
                    getAllFiles(filePattern);
                }
            }
        }
        return fileList;
    }
}



JAVA的List自定义排序

public class ComparatorFileSize implements Comparator {

    @Override
    public int compare(FileSort x, FileSort y) {
        if (x.getFileSize() > y.getFileSize()) {
            return 1;
        } else if (x.getFileSize() == y.getFileSize()) {
            return 0;
        } else {
            return -1;
        }
    }


调用

Collections.sort(sortList, new ComparatorFileSize());


其它和C#也没啥不同了,NetBeans做swing真的很方便,和.net没很大区别    

注:很好奇地在两个软件上测试了一下性能,为啥C#比java快得多?C#耗时只有java一个零头?难道java自己弄的模糊查找效率比.net下FileInfo、 DirectoryInfo那些现成方法差很多很多.....


你可能感兴趣的:(java)