Java replication Specifies the suffix name file and modifies the prefix name

Source: Internet
Author: User

Import Java.io.file;import java.io.fileinputstream;import java.io.fileoutputstream;import java.io.FilenameFilter; Import java.util.arraylist;import java.util.list;/** * Copy all the. java files under the C:\CN directory to the C:\Test directory and change the extension of the original file from. java to. wl.  * @author WL * @time 2014.10.05 02:10:23 * */public class Copyfilesfromatob {public static void main (string[] args) throws Exception {myfilter myfilter=new myfilter (); File F1=new file ("C:\\cn"); File F2=new ("C:\\test"); Copyfilesandrename (f1,f2,myfilter);//Copy the files ending in. java under F1 directory to directory F2 and modify their extension}public static void Copyfilesandrename (File f1,file f2,filenamefilter myfilter) throws exception{list<file> list=new ArrayList <File> (), if (!f1.exists () &&f1.isdirectory ()) {throw new RuntimeException ("directory does not Exist");} if (!f2.exists () &&f2.isdirectory ()) {f2.mkdir ();//If F2 does not exist create a new}file[] files=f1.listfiles ();/ List all files under F1 list=recarsive (files,myfilter,list);//traverse all files under file and add the files ending in. java to the list; FileInputStream fis; FileOutputStream Fos; String filename;//iterates through the file in the list,Copy to F2, and modify the extension for (File file:list) {filename =file.getname (). replace (". Java", ". WL"); File Newfile=new file (f2,filename); fis=new FileInputStream (File); fos=new FileOutputStream (NewFile); FileCopy (FIS, FOS); Fis.close (); Fos.close ();}} Copy file contents private static void FileCopy (FileInputStream fis, FileOutputStream fos) throws Exception {int len=0;byte[] Buf=ne W Byte[1024];if ((Len=fis.read (BUF))!=-1) {fos.write (buf, 0, Len);}} Recursively traverse the directory to get the collection of all files ending in. java listpublic static list<file> recarsive (file[] Files,filenamefilter myfilter,list <File> list) {for (File file:files) {if (File.isdirectory ()) {recarsive (File.listfiles (), myfilter,list);} else if (myfilter.accept (file, File.getname ())) {List.add (file);}} System.out.println ("List.size ()" +list.size ()); return list;}} File filter class, filter all files ending in. Java class Myfilter implements filenamefilter{@Overridepublic Boolean accept (file dir, String name) {return Name.endswith (". Java");}}

Java replication Specifies the suffix name file and modifies the prefix name

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.