Original: Java bulk modify the code for all suffix names in the specified folder with a different suffix
Source code: Http://www.zuidaima.com/share/1550463660264448.htm
Today there is a need to put a folder under all the suffix named JSP changes to FTL, originally wanted to use bat to implement the BAT's advanced syntax is also not familiar with, and later found that also need to recursively traverse all subfolders, so Java implemented a function of the same code, There is a need for a cow who can download the changes for what they want.
This can be compatible with Windows and Linux.
Package Com.zuidaima.fileutil;import Java.io.file;import Java.util.arraylist;import java.util.arrays;import java.util.list;/**** @author Www.zuidaima.com**/public class Batchrename {public static void main (string[] args) {String dir = "c:/jsp/"; File File = new file (dir); String Srcsuffix = "JSP"; String dstsuffix = "FTL"; list<string> paths = Listpath (file, Srcsuffix); for (String path:paths) {File Srcfile = new file (path); String name = Srcfile.getname (); int idx = Name.lastindexof ("."); String prefix = name.substring (0, IDX); File Dstfile = new file (srcfile.getparent () + "/" + prefix + "." + Dstsuffix), if (Dstfile.exists ()) {//Here code note Changes to the logic you want Srcfile.delete (); continue;} Srcfile.renameto (Dstfile);}} /** * Gets all eligible paths under the specified path */private static list<string> Listpath (File path, String acceptsuffix) {list<string> List = new arraylist<string> (); file[] files = path.listfiles (); Arrays.sort (Files), for (File file:files) {if (File.isdirectory ()) {list<string> _list = ListpatH (file, Acceptsuffix); List.addall (_list);} else {String name = File.getname (); int idx = Name.lastindexof ("."); String suffix = name.substring (idx + 1), if (Suffix.equals (Acceptsuffix)) {List.add (File.getabsolutepath ());}}} return list;}}
Java bulk modifies code with a different suffix for all suffix names under the specified folder