Here you can find the source of renameFiles(File file, String filterRegex, String replaceRegex, String replacement)
Parameter | Description |
---|---|
file | a parameter |
filterRegex | a parameter |
replaceRegex | a parameter |
replacement | a parameter |
public static boolean renameFiles(File file, String filterRegex, String replaceRegex, String replacement)
//package com.java2s; import java.io.File; public class Main { /**//ww w .ja v a 2 s . c o m * recursively rename all sub files while matches filterRegex, replace each * substring of filename that matches replaceRegex with the given * replacement. * @param file * @param filterRegex * @param replaceRegex * @param replacement * @return */ public static boolean renameFiles(File file, String filterRegex, String replaceRegex, String replacement) { if (!file.exists()) return true; // recursively traversal all sub files if (file.isDirectory()) { File[] subFiles = file.listFiles(); for (File subFile : subFiles) { if (!renameFiles(subFile, filterRegex, replaceRegex, replacement)) return false; } } else if (file.getName().matches(filterRegex)) { String newFileName = file.getName().replaceAll(replaceRegex, replacement); String directory = file.getParent(); return file.renameTo(new File(directory + File.separator + newFileName)); } return true; } public static boolean renameFiles(File file, String regex, String replacement) { return renameFiles(file, ".*", regex, replacement); } }