IO流拷贝文件和目录

使用IO流来复制文件的练习

这里关键是使用了一个递归的方法

import java.io.*;

public class CopyAll {
    public static void main(String[] args) {
        //拷贝源
        File srcfile=new File("F:\\EV\\one");
        //拷贝位置
        File destfile=new File("F:\\EV\\two");
        copy(srcfile,destfile);
    }
    private static void copy(File srcfile,File destfile){
            if(srcfile.exists()){//判断路径是否存在
                if(srcfile.isFile()){//判断源文件是否是一个文件
                    //创建文件字节流(万能流)
                    FileInputStream fis=null;
                    FileOutputStream fos=null;
                    try {
                        fis=new FileInputStream(srcfile);
                        fos=new FileOutputStream(destfile.getAbsoluteFile()+"//"+srcfile.getName());
                        //每秒1mb的速度复制
                        byte[] bytes=new byte[1024*1024];
                        int readDate=0;
                        while ((readDate=fis.read(bytes))!=-1){
                            //边读边写
                            fos.write(bytes,0,readDate);
                        }
                        fos.flush();
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }finally {
                        if(fis!=null){
                            try {
                                fis.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                        if(fos!=null){
                            try {
                                fos.close();
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }
                    }return;
                }else{
                    //如果为目录的话获取目录下的所有子文件
                    File[] files=srcfile.listFiles();
                    for (File f:files) {
                        //遍历判断是否是子文件下是否有目录
                        if(f.isDirectory()){
                            //有目录的话就获取目录的路径
                            String desDir=destfile.getAbsolutePath()+"\\"+f.getName();
                            //这里创建一个新的目录文件夹
                            File newFile=new File(desDir);
                            if(!newFile.exists()){
                                newFile.mkdir();
                            }//然后使用递归获取下一个目录的文件
                            copy(f,newFile);
                        }else {//如果是文档的话递归直接复制出来
                            copy(f,destfile);
                        }
                    }
                }
            }else{
                System.out.println("路径错误!!!!");
            }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值