Copy the specified suffix file in java and modify its suffix

Source: Internet
Author: User

Copy the specified suffix file in java and modify its suffix

Import java. io. file; import java. io. fileInputStream; import java. io. fileOutputStream; import java. io. filenameFilter; import java. util. arrayList; import java. util. list;/*** set all the values in the c: \ cn directory. copy the java file to the c: \ test directory, and copy the original file extension from. change java. 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 File ("C: \ test"); copyFilesAndReName (f1, f2, myFilter ); // run. copy the File ending with java to the directory f2 and modify its extension} public static void copyFilesAndReName (File f1, File f2, FilenameFilter myFilter) throws Exception {List
 
  
List = new ArrayList
  
   
(); If (! F1.exists () & f1.isDirectory () {throw new RuntimeException ("the directory does not exist");} if (! F2.exists () & f2.isDirectory () {f2.mkdir (); // create a new File if f2 does not exist.} File [] files = f1.listFiles (); // list all files under f1 = recarsive (files, myFilter, list); // traverse all files under files and. add the File ending with java to the List; FileInputStream FCM; FileOutputStream fos; String filename; // traverse the file in the List, copy it to f2, and modify the extension for (File file: list) {filename = file. getName (). replace (". java ",". wl "); File newfile = new File (f2, filename); FD = new FileInputStream (file); fos = new Fil EOutputStream (newfile); fileCopy (FCM, fos); FCM. close (); fos. close () ;}// copy the file content private static void fileCopy (FileInputStream ISI, FileOutputStream fos) throws Exception {int len = 0; byte [] buf = new byte [1024]; if (len = Fi. read (buf ))! =-1) {fos. write (buf, 0, len) ;}// recursively traverse the directory to obtain the listpublic static List of all objects ending with. java.
   
    
Recarsive (File [] files, FilenameFilter myFilter, List
    
     
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. class MyFilter implements FilenameFilter {@ Overridepublic boolean accept (File dir, String name) {return name. endsWith (". java ");}}
    
   
  
 

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.