Java implementation file Batch rename

Source: Internet
Author: User

The File class (document management) and the pattern class (regular match) are used.

Regular matching of the original file name

New file name with serial number

1 ImportJava.io.File;2 ImportJava.io.FilenameFilter;3 ImportJava.util.regex.Pattern;4 5  Public classRename6 {7      Public StaticString url= "d:\\mb\\2\\";8     //The dir files9      Public StaticString namerule= "2.*";Ten     //The rule for finding files One      Public StaticString renamerule= "2.part#.r-a-r"; A     //#represent the new name index of files in [1,n], n are the num of old files, by ASC order -      Public Static  voidMain (String []args) -     { theFile file=NewFile (URL); -File []files=file.listfiles (NewFilenameFilter () { - @Override -              Public BooleanAccept (File dir, String name) { +                 returnpattern.matches (namerule,name); -             } +         }); ASystem.out.println ("There is" +files.length+ "(s) files found!"); atSystem.out.println ("Rename begin!"); -String []s=renamerule.split ("#"); -  -          for(inti=0;i<files.length;i++) -         { -File newf=NewFile (url+s[0]+ (i+1) +s[1]); in Files[i].renameto (NEWF); -         } toSystem.out.println ("Rename complete!"); +     } -}

Test file:

Operation Result:

Java implementation file Batch rename

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.