Java复制文件

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

public class Test {

        public static void main(String[] args) throws IOException {

                File srcForder = new File("d:\\");

                File destForder = new File("d:\\Test\\");

                isFile(srcForder, destForder);
        }

        private static void isFile(File srcForder, File destForder) throws IOException {

                if (!destForder.exists())

                        destForder.mkdir();

                File[] file = srcForder.listFiles();

                if (file != null) {

                        for (File srcFile : file) {

                                if (srcFile.isDirectory()) {

                                        isFile(srcFile, destForder);

                                } else {

                                        if (srcFile.getName().endsWith(".java")) {
                                                
                                                File destFile = new File (destForder,srcFile.getName()) ;

                                                copyFile(srcFile, destFile);
                                        }
                                }
                        }
                }
        }

        private static void copyFile(File srcFile, File destFile) throws IOException {

                BufferedReader br = new BufferedReader(new FileReader(srcFile));

                BufferedWriter bw = new BufferedWriter(new FileWriter(destFile)) ;

                String line = null ;
                while ((line = br.readLine()) != null) {

                        bw.write(line);
                        bw.newLine();
                        bw.flush();
                }

                br.close();
                bw.close();
        }
}

posted @ 2015-01-12 13:59  静以养身 俭以养德  阅读(123)  评论(0编辑  收藏  举报