/** * 文件重命名 * 新名称的规则可自行在代码里修改 * @param filePath 要批量命名的文件根目录,如 D:/test ,就是将D:/test目录下的所有文件都重命名了 * @param newFileName 新的名称,如 原名称为 test.txt 和 other.txt,重命名后变为 test[1].txt 和 other[2].txt */ public static void fileRename(String filePath, String newFileName) { File path = new File(filePath); if (path.isDirectory()) { File[] files = path.listFiles(); int cnt = 0; for (int i = 0; i < files.length; i++) { cnt++; File file = files[i]; if (file.isFile()) { String realName = file.getName(); String Postfix = realName.substring(realName.indexOf('.')); File dest = new File(filePath + "/" +newFileName + "[" + cnt + "]" + Postfix); file.renameTo(dest); } } System.out.println("\n重命名完成!"); } else { System.out.println("\n您输入的不是目录!"); } }