java拷贝目录到其他盘

 
import java.io.*;

public class CreateNode {
    public static void main(String[] args) {
        File src = new File("D:\\C");
        File dost = new File("C:\\");
        CopyFiles(src,dost);
    }

    private static void CopyFiles(File src, File dost) {
        if(src.isFile()) {
            FileInputStream fis=null;
            FileOutputStream fos=null;
            try {
                 fis= new FileInputStream(src);
                 String srcFile = src.getAbsolutePath();
                 String dostFile = dost.getAbsolutePath()+srcFile.substring(3);
                 fos= new FileOutputStream(dostFile);
                 byte[] bytes = new byte[1024*1024];
                 int index;
                 while((index = fis.read(bytes))!=-1){
                     fos.write(bytes,0,index);
                 }
                 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;
        }
        File[] files = src.listFiles();
        for (File file:files) {
            if(src.isDirectory()){
                String srcFile = src.getAbsolutePath();
                String dostFile = dost.getAbsolutePath()+srcFile.substring(3);
                File newFile = new File(dostFile);
                if(!newFile.exists()){
                    newFile.mkdirs();
                }
            }
            CopyFiles(file,dost);

        }
    }
}
public static void main(String[] args) {
        File src = new File("D:\\C");源目标盘文件
        File dost = new File("C:\\");目标目标盘文件
        CopyFiles(src,dost);拷贝文件方法
    }

创建源目录和目标目录
拷贝方法实现:
方法存在递归:
分两段展示:


        File[] files = src.listFiles();//读取文件目录的数组 每个元素是个file文件
        for (File file:files) {// for循环遍历文件
            if(src.isDirectory()){ //判断文件是否为文件夹 如果是文件夹 那就在目标盘下创建文件目录
                String srcFile = src.getAbsolutePath();//获取该目录的绝对路径
                //假如源目录盘是F:\\那目标盘是C:\\想要在目标盘创建目录就需要改路径
                String dostFile = dost.getAbsolutePath()+srcFile.substring(3);
目标盘dost假如是C:\\那源路径就改为 目标盘路径+源目录拼接字符串的第四个下标开始
                File newFile = new File(dostFile);创建目标路径
                if(!newFile.exists()){//判断是否存在该目录 如果不存在那就新创建
                    newFile.mkdirs();
                }
            }
            CopyFiles(file,dost);//递归方法 读取目录的第一个文件

        }
    }
        if(src.isFile()) {//递归目录下的第一个文件 判断该文件是不是文件夹 如果不是
            FileInputStream fis=null; //读取源目录
            FileOutputStream fos=null;//输入到目标目录
            try {
                 fis= new FileInputStream(src);
                 String srcFile = src.getAbsolutePath(); //拼接字符串复制文件到目标目录的路径
                 String dostFile = dost.getAbsolutePath()+srcFile.substring(3);
                 fos= new FileOutputStream(dostFile);
                 byte[] bytes = new byte[1024*1024];//创建数组准备读取源目录文件
                 int index;//索引
                 while((index = fis.read(bytes))!=-1){
                     fos.write(bytes,0,index);
                 }
                 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;
        }

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值