批量改文件夹后缀名
想要打印照片,发现有些照片格式不统一。
public static void main(String[] args) {
String path = "文件路径" ;
String from = "webp";
String to = "jpg";
suffixRename(path, from, to);
}
/**
*
* @param path 要批量修改后缀名文件夹路径
* @param from 源文件后缀名
* @param to 修改后目标文件后缀名
*/
public static void suffixRename(String path, String from, String to) {
File file = new File(path);
File[] fs = file.listFiles();
for (int i = 0; i < fs.length; i++) {
File f2 = fs[i];
if (f2.isDirectory()) {
suffixRename(f2.getPath(), from, to);
} else {
String name = f2.getName();
if (name.endsWith(from)) {
f2.renameTo(new File(f2.getParent() + File.separator
+ name.substring(0, name.indexOf(from)) + to));
}
}
System.out.println("已修改第"+i+"张");
}
System.out.println("修改完成!");
}

浙公网安备 33010602011771号