Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement