Java实现拷贝文件夹

14 篇文章 1 订阅
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileCopyAllTest {
    public static void main(String[] args) {

        File srcDir = new File("C:\\Tools");
        File targetDir = new File("C:\\Tool");
        //调用复制指定目录下所有文件的方法
        copyDir(srcDir, targetDir);
    }


    /**
     * 复制文件夹的方法
     * @param srcDir:源文件目录
     * @param targetDir:目的文件目录
     */
    public static void copyDir(File srcDir,File targetDir){
        if(!targetDir.exists()){
            targetDir.mkdir();//如果目的地的目录不存在,则需要使用File类的方法进行创建目录
        }
        File[] files = srcDir.listFiles(); //获取指定目录下的所有File对象
        for (File file : files) {
            if (file.isFile()) {
                copyFile(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
            }else{
                //不是继续调用该方法判断,使用递归实现
                copyDir(new File(srcDir+"\\"+file.getName()), new File(targetDir+"\\"+file.getName()));
            }
        }
    }

    /**
     * 复制文件的方法
     * @param srcFile:源文件
     * @param targetFile:目的文件
     */
    public static void copyFile(File srcFile,File targetFile){
        //(1)提高读取效率,从数据源
        BufferedInputStream bis = null;
        //(2)提高写入效率,写到目的地
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new FileInputStream(srcFile));
            bos = new BufferedOutputStream(new FileOutputStream(targetFile));

            //(3)边读边写
            byte[] buf = new byte[1024];//中转站
            int len = 0;//用于存储读到的字节的个数
            while((len=bis.read(buf))!=-1){
                bos.write(buf,0,len);
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //(4)关闭
            if(bos!=null){
                try {
                    bos.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if(bis!=null){
                try {
                    bis.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }

        }
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
以下是使用Java的方法来拷贝文件夹的示例代码: ```java import java.io.File; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; public class FolderCopyExample { public static void main(String[] args) { // 源文件夹路径 String sourceFolder = "C:\\Users\\username\\sourceFolder"; // 目标文件夹路径 String targetFolder = "C:\\Users\\username\\targetFolder"; try { // 创建目标文件夹 Path targetPath = Paths.get(targetFolder); if (!Files.exists(targetPath)) { Files.createDirectories(targetPath); } // 拷贝文件夹 Path sourcePath = Paths.get(sourceFolder); Files.walk(sourcePath).forEach(source -> { Path target = targetPath.resolve(sourcePath.relativize(source)); try { Files.copy(source, target); } catch (IOException e) { System.err.println("Failed to copy " + source + " to " + target + ": " + e.getMessage()); } }); System.out.println("Folder copied successfully."); } catch (IOException e) { System.err.println("Failed to copy folder: " + e.getMessage()); } } } ``` 在此示例中,我们使用Java的Path和Files类来遍历源文件夹中的所有文件和子文件夹,并将它们复制到目标文件夹中。我们还使用Files.createDirectories()方法创建目标文件夹。 这是一个简单的方法,可以在大多数情况下很好地工作,但在处理大型文件夹时可能会遇到性能问题。在这种情况下,您可能需要使用更高级的技术,如多线程处理。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

梧杵

还是学生,生活太难

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

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

打赏作者

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

抵扣说明:

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

余额充值