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;
}
}
点击查看更多内容
1人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦