java实现文件夹的复制(包括子文件夹以及子文件)

JAVA实现文件夹的复制

思路分析

实现文件夹的复制,需要考虑到文件夹和文件的差别。如果是文件,则可以直接利用java中io流进行文件的复制,如果是文件夹,则需要考虑源文件夹的路径,在目标位置创建相应文件夹,进行递归处理。

代码展示

package com.java.study;

import java.util.*;
import java.io.*;



public class Test {
    private String src;
    private String target;

    public Test(String src, String target) {
        this.src = src;
        this.target = target;
    }

    public String getSrc() {
        return src;
    }

    public void setSrc(String src) {
        this.src = src;
    }

    public String getTarget() {
        return target;
    }

    public void setTarget(String target) {
        this.target = target;
    }


    //复制文件,后续大家可以考虑缓冲问题,减少io次数,从而提高效率
    static void copyfile(String oldfile,String newfile) throws IOException {

        File oldf=new File(oldfile);
        FileOutputStream fop=new FileOutputStream(new File(newfile));
        FileInputStream fip=new FileInputStream(oldf);
        int count=fip.available();
        byte[] bytes=new byte[count];
        int input=-1;
        while((input=fip.read(bytes))!=-1){
            fop.write(bytes);
        }
        fop.flush();
        fop.close();
        fip.close();
    }


    //复制文件夹
    static void copydir(String oldpath,String newpath) throws IOException {
        File f1=new File(oldpath);
        File[] files=f1.listFiles();
        for(File ff:files){
            if(!ff.isDirectory()){   //如果是文件,直接调用上面文件复制方法进行复制
                String newtarget=newpath+File.separator+ff.getName();
                copyfile(ff.getAbsolutePath(),newtarget);
            }else{    //如果是文件夹,运用递归进行文件夹的复制
                String newtarget=newpath+File.separator+ff.getName();
                File newdir=new File(newtarget);
                newdir.mkdir();
                copydir(oldpath+File.separator+ff.getName(),newpath+File.separator+ff.getName());
            }
        }
    }



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


        copydir("f:\\java1","f:\\target");

    }






}

  • 1
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
可以使用Java的IO库中的File类来实现文件夹复制,具体可以参考以下代码: ``` import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.nio.channels.FileChannel; public class FolderCopy { public static void main(String[] args) throws IOException { File source = new File("sourceFolder"); File dest = new File("destFolder"); // 如果目标文件夹不存在,则创建 if (!dest.exists()) { dest.mkdir(); } // 获取源文件夹下的所有文件文件夹 File[] files = source.listFiles(); // 遍历文件复制 for (File file : files) { if (file.isDirectory()) { // 递归复制文件夹 copyFolder(file.getPath(), dest.getPath() + File.separator + file.getName()); } else { // 复制文件 FileChannel in = null; FileChannel out =null; try { in = new FileInputStream(file).getChannel(); out = new FileOutputStream(dest.getPath() + File.separator + file.getName()).getChannel(); in.transferTo(0, in.size(), out); } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } } System.out.println("文件夹复制完成!"); } public static void copyFolder(String sourceDir, String targetDir) throws IOException { (new File(targetDir)).mkdirs(); File[] file = (new File(sourceDir)).listFiles(); for (int i = 0; i < file.length; i++) { if (file[i].isFile()) { File sourceFile = file[i]; File targetFile = new File(new File(targetDir).getAbsolutePath() + File.separator + file[i].getName()); FileInputStream in = new FileInputStream(sourceFile); FileOutputStream out = new FileOutputStream(targetFile); byte[] buffer = new byte[1024 * 5]; int len; while ((len = in.read(buffer)) != -1) { out.write(buffer, 0, len); } out.flush(); out.close(); in.close(); } if (file[i].isDirectory()) { String sourcePath = sourceDir + "/" + file[i].getName(); String targetPath = targetDir + "/" + file[i].getName(); copyFolder(sourcePath, targetPath); } } } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

我的大男子主义

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值