Java bulk modifies the code for all suffix names under the specified folder with a different suffix

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.