11ImportJava.io.File;22ImportJava.util.Scanner;33//Bulk Replace file name characters44 Public classTest07 {56 Public Static voidReName () {67 while(true) {78 Scanner sc =NewScanner (system.in);89 String FilePath = "";//Input Path9Ten File File =NewFile ("");//File ObjectTenOne String Dirpath =NULL;//Absolute Path One12//isdirectory () tests whether the file represented by this abstract path name is a directory A13 while(!file.isdirectory ()) { -System.out.println ("Please enter the folder you want to view (E:\\io)"); -FilePath = Sc.nextline () + "\ \"; theFile =NewFile (filePath); -17//GetAbsolutePath () returns the absolute pathname form of this abstract path name. -Dirpath =File.getabsolutepath (); -19 } +20System.out.println (dirpath); -21st//returns an abstract path to the an array group that represents the files in the directory represented by this abstract path name. +file[] FileList =file.listfiles (); ASYSTEM.OUT.PRINTLN ("The path you entered has the following file:"); at24 for(File file2:filelist) {//using the Foreach Loop body -25if(File2.isfile ()) {//if it is a file, get the filename and output -String Formfile =file2.getname (); -27System.out.println (formfile); -28 } -29 } inSystem.out.println ("Please enter the old character you want to replace:"); -String Oldstr =sc.nextline (); toSystem.out.println ("Please enter the new character you want to replace:"); +Newstr String =sc.nextline (); -34intCount = 0;//declares the count of an int to record the number of files that have been successfully replaced the35 for(inti = 0; i < filelist.length; i++) { *The String name = Filelist[i].getname ();//get the filename of each file $PNS name = Name.replace (Oldstr, NEWSTR);//Replace a file name that contains oldstr charactersPanax Notoginseng$ name = Dirpath + "\ \" + Name;//file name -ToFile File =NewFile (name);//convert name to file object the40if(Filelist[i].exists () &&!tofile.exists ()) {//new file does not exist to change +Filelist[i].renameto (ToFile);//rename (); File method, user renaming Acount++;//number of successful files replaced +1 the43 } +44 } -SYSTEM.OUT.PRINTLN ("Replace successful, Total Files:" + Filelist.length + ", success" +Count $46 + "single, Failed" + (Filelist.length-count) + "one." "); $System.out.println ("The failed file is:"); -48 for(File file2:filelist) {//the output does not replace a successful file name -49if(File2.isfile ()) { theString Formfile =file2.getname (); -Wuyi System.out.println ("\ T" +formfile);Wuyi52 } the53 } -54sc.close (); WuSystem.out.println ("closed after 3 seconds"); -56NewThread () {//Create a new thread, stop for three seconds and then output About57@Override $58 Public voidrun () { -59Try { -Sleep (3000); -61}Catch(interruptedexception e) { A62e.printstacktrace (); +63 } the64 } -65}.start (); $66 Break; the67 } the68 } the69 Public Static voidMain (string[] args) { the70reName (); -71 } in72 } the73 the
View Code
This Java program is mainly to learn the Java IO operation when the file understanding, and then think of writing a replacement character program.
Bulk Replace file characters