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 ");}}