Before learning the file I would like to learn the name of the video changed, because the filename is too long to see, then try to write a functional implementation
1 PackageCom.gh.file;2 3 ImportJava.io.File;4 5 /**6 * Batch file naming7 * 8 * @authorGanhang9 * Ten */ One Public classFilerename { A Public StaticString filepath = "f:\\ learning \\JAVA\\JAVA_SE";//The address of the folder to be renamed in bulk - Public Static voidMain (string[] args) { -File File =NewFile (filepath); thefile[] Filearrays =file.listfiles (); - //number of output files - System.out.println (filearrays.length); - for(File f:filearrays) + { -String filename =f.getname (); + intLen=0; AString NewFileName =NULL; at if(Filename.startswith ("**********java. SE Video ") {//Avoid advertising with * instead of -Len= "**********java. SE Video ". Length (); -Newfilename= "Java" +filename.substring (len); - } - Else if(Filename.startswith ("******_java Foundation")){ -len= "******_java Foundation". Length (); inNewfilename= "Java" +filename.substring (len); - } toF.renameto (NewFile (Filepath+file.separator+newfilename));// Renaming + } -file[] fileArrays1 = File.listfiles ();//output file name to see the for(File fs:filearrays1) { * System.out.println (Fs.getname ()); $ }Panax Notoginseng } -}
There are still two homework to fill in the back.
1 Packagecom.gh.homework;2 3 ImportJava.io.File;4 5 /**6 * Find files in the specified directory ending with the specified suffix name;7 * 8 * @authorGanhang9 * Ten */ One Public classFiledemo { A Public Static voidfindfile (file file, String extsname) { - if(File = =NULL) - return; the if(File.isdirectory ()) { -file[] fs =file.listfiles (); - if(FS! =NULL) { - for(File file2:fs) { +FindFile (File2, extsname);//recursive implementation; - } + } A}Else { atString Path =File.getpath (); - if(Path.endswith (extsname)) { - System.out.println (File.getpath ()); - } - } - } in - //Find all TXT file output path under D disk; to Public Static voidMain (string[] args) { +File File =NewFile ("d:\\"); -String extsname = ". txt"; the findfile (file, extsname); * } $}
Packagecom.gh.homework;ImportJava.io.File;/*** Delete files that end with the specified suffix in the specified directory;@authorGanhang **/ Public classFiledeletedemo { Public Static voidDeleteFile (file file, String extsname) {if(File = =NULL) return; if(File.isdirectory ()) {if(File.length () ==0) {file.delete (); System.out.println ("Deleted" +File.getpath ()); } file[] FS=File.listfiles (); if(FS! =NULL) { for(File file2:fs) {DeleteFile (file2, extsname);//recursive implementation; } } } Else{String path=File.getpath (). toLowerCase (); if(Path.endswith (extsname)) {//File.delete (); //System.out.println ("deleted" +file.getpath ()); } } } //Delete all TXT file output path under D drive; Public Static voidMain (string[] args) {File file=NewFile ("f:\\"); String Extsname= ". txt"; DeleteFile (file, extsname); }}
Java Learning IO II