继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

重命名目录下的所有文件

car
关注TA
已关注
手记 83
粉丝 56
获赞 363
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;
import java.util.regex.Pattern;
public final class Directory {
    private static File[] local(File dir, final String regex) {
        return dir.listFiles(new FilenameFilter() {
            final 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>();
        private List<File> dirs = new ArrayList<File>();
        @Override
        public Iterator<File> iterator() {
            // TODO Auto-generated method stub
            return files.iterator();
        }
        void addAll(TreeInfo others) {
            files.addAll(others.files);
            dirs.addAll(others.dirs);
        }
    }
    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) {
        List<String> list = getFileNames("F:\\");
        List<String> list2 = getFileNames("D:\\PDF\\");
        for (int i = 0; i < list.size(); i++) {
            File file2=new File("D:\\PDF\\"+list2.get(i));
            File file=new File("D:\\PDF\\"+list.get(i));        
            System.out.println(file2.renameTo(file));
        }
    }
    private static List<String> getFileNames(String path) {
        TreeInfo sourceInfo = walk(path);
        List<File> files = sourceInfo.files;
        File file;
        String fileName;
        List<String> list = new ArrayList<>();
        for (int i = 0; i < files.size(); i++) {
            file = files.get(i);
            if (file.isFile()) {
                fileName = file.getName();
                if (fileName.indexOf(".") != -1)
                    list.add(fileName);
            }
        }
        return list;
    }
}
打开App,阅读手记
1人推荐
发表评论
随时随地看视频慕课网APP