Skip to content

Instantly share code, notes, and snippets.

@keepingcoding
Created June 5, 2019 16:34
Show Gist options
  • Save keepingcoding/eaf9c2cede1bbc14e501b8a00fa3ef48 to your computer and use it in GitHub Desktop.
Save keepingcoding/eaf9c2cede1bbc14e501b8a00fa3ef48 to your computer and use it in GitHub Desktop.
批量修改文件名
import java.io.File;
/**
* 批量修改文件名
*
* @author zzs
*/
public class ChangeFileName {
public static void main(String[] args) {
// 修改该目录下的文件
File file = new File("E:/tmp/movie");
// 列出所有文件与目录
File[] files = file.listFiles();
int prefix = 0;
for (File f : files) {
if (f.isDirectory()) {
continue;
}
// 获取原先的文件名
String oldName = f.getName();
System.out.println(oldName);
// 组装成新的文件名
// String newName = (++prefix) + oldName;
String newName = prefix + oldName;
System.out.println("---" + newName);
// 使用新的文件名构建File对象
File newFile = new File(f.getParent() + "/" + newName);
// 重命名
boolean res = f.renameTo(newFile);
System.out.println("---" + res);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment