Java Learning IO II

Source: Internet
Author: User

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

Related Article

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.