下了一些文件,想批量改名,于是想到用Java写点代码,正好来练习一下,
import java.io.File;
import java.io.FilenameFilter;
import java.io.IOException;
class D{
public static void main(String[] args) throws IOException {
File dirPath = new File("E:\\A\\B");
changeName(dirPath);
}
public static void changeName(File dirPath) {
File[] files = dirPath.listFiles(new FilenameFilter() {//用了文件名过滤器
@Override
public boolean accept(File dir, String name) {
if(name.toLowerCase().endsWith(".exe"))
return true;
return false;
}
});
for(File file : files){
String regex = "(.{2}).{17}(.*)";//将第3到19个字符去掉
String newName = file.getName().replaceAll(regex, "$1$2");
File dest = new File(file.getParentFile(),newName);//不加路径的话,文件会移动到java运行目录中
System.out.println(newName);
file.renameTo(dest);
}
}
}
本文介绍了一个使用Java实现的批量文件重命名程序。该程序能够筛选出特定目录下的.exe文件,并按照预设的正则表达式规则进行批量重命名。通过这个简单的例子,读者可以了解到如何利用Java操作文件系统。
1591

被折叠的 条评论
为什么被折叠?



