1 /*2 Requirements :
* Change the name of the video below H:\ kingdoms to3 00?_ Introduction. avi4 5 Ideas:6 A: Package directory7 B: Gets the file array for all files in this directory8 C: Traverse the file array to get each file object9 D: Stitch A new name, then rename it. Ten */ One PackageZl_file; A - ImportJava.io.File; - Public classFileTest3 { the - Public Static voidMain (string[] args) { - - //Package Catalog +File Forder =NewFile ("H:\\ Kingdoms"); - + //get file array for all files in this directory Afile[] FileList =forder.listfiles (); at - //iterate over the file array to get each file object - for(File file:filelist) { - //Kingdoms _001_[Storytelling net]_ Taoyuan three jieyi. avi changed to 001_ Taoyuan three jieyi. avi - //first get the name -String name =file.getname (); in //intercept the part of the name that you want to modify - //First Intercept ' 001 ' out, and its left is _, and then intercept 3 characters from _ to //index position of first fetch _ + intIndex1 = Name.indexof ("_"); -String newname1 = name.substring (index1 + 1, index1+4);//package left without packet right the //System.out.println (newname1); * $ //then intercept ' Taoyuan three jieyi. avi ' OutPanax Notoginseng intIndex2 = Name.lastindexof ("_"); -String newname2 = name.substring (INDEX2);//from _ To the end, intercept it. the + //Stitching AString result =Newname1.concat (newname2); the System.out.println (result); + /* - 001_ Taoyuan three jieyi. avi $ 002_ 10 The usual mess of the waiter. avi $ 003_ in the Battle of Lu bu. avi - The Battle of 004_ Chibi. avi - */ the } - Wuyi the } -}
Java 19-15 file class How to change file names in bulk