A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘闯2 中级黑马   /  2017-12-18 21:53  /  1133 人查看  /  4 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

今天学习的重难点:
//对文件夹的复制
public class Demo09 {
        public static void main(String[] args) throws IOException {
                File src = new File("D:\\asrc");
                File dest=new File("D:\\adest");
                dest.mkdir();
                copyFile(src,dest);
        }
        public static void copyFile(File src,File dest) throws IOException {
                if (src.isDirectory()&&dest.isDirectory()) {
                        //创建文件夹
                        File f=new File(dest,src.getName());//得到的是根文件夹D:\\adest \\asrc
                        if (!f.exists()) {
                                f.mkdir();
                        }
                        File[] files = src.listFiles();
                        for (File file : files) {
                                if (file.isFile()) {
                                        File ff = new File(f,file.getName());
                                        BufferedReader br=new BufferedReader(new FileReader(file.getAbsolutePath()));
                                        BufferedWriter bw=new BufferedWriter(new FileWriter(ff));
                                        String line;
                                        while((line=br.readLine())!=null){
                                                bw.write(line);
                                                bw.newLine();
                                                bw.flush();
                                        }
                                        bw.close();
                                        br.close();
                                }else {
                                        copyFile(file,f);
                                }
                        }
                       
                }       
        }
}

4 个回复

倒序浏览
回复 使用道具 举报
回复 使用道具 举报
不错不错    杠杠的   加油
回复 使用道具 举报
杠杠的   加油
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马