iis服务器助手广告广告
返回顶部
首页 > 资讯 > 后端开发 > Python >java实现系统多级文件夹复制
  • 850
分享到

java实现系统多级文件夹复制

2024-04-02 19:04:59 850人浏览 独家记忆

Python 官方文档:入门教程 => 点击学习

摘要

本文实例为大家分享了java实现系统多级文件夹复制的具体代码,供大家参考,具体内容如下 package com.jae; import java.io.*; //复制文件夹内

本文实例为大家分享了java实现系统多级文件夹复制的具体代码,供大家参考,具体内容如下


package com.jae;

import java.io.*;

//复制文件夹内的内容,包含多级文件夹
public class Test2 {
    public static void main(String[] args) throws Exception {
        //原文件夹地址
        File resPath = new File("E:\\Java\\分享");
        File destPath = new File("E:\\");
        //method(resPath, destPath);
        copy(resPath, destPath);
    }
    public static void copy(File src,File dest) throws Exception {
        File newDir = new File(dest,src.getName());
        newDir.mkdir();
        File[] subFiles = src.listFiles();
        for (File subFile : subFiles) {
            if(subFile.isFile()){
                BufferedInputStream bis = new BufferedInputStream(new FileInputStream(subFile));
                BufferedOutputStream bos =
                        new BufferedOutputStream(
                                new FileOutputStream(
                                        new File(newDir,subFile.getName())));
                int b;
                byte[] bytes = new byte[1024 * 8];
                while((b = bis.read(bytes)) != -1){
                    bos.write(bytes,0,b);
                }
                bis.close();
                bos.close();
            }else{
                copy(subFile,newDir);//递归调用
            }

        }
    }
    public static void method(File dir, File destPath) throws IOException {
        //获取源路径的文件
        File[] files = dir.listFiles();
        //根目录文件夹名
        String name1 = dir.getName();
        //遍历源路径的文件
        for (File file : files) {
            if (file.isFile()) {

                StringBuilder sb = new StringBuilder(destPath.getAbsolutePath());
                sb.append("\\").append(file.getName());
                //获取盘符后面的路径和文件名
                //String s = absolutePath.split(":")[1];

                //创建输入流,封装获取到的文件的绝对路径
                FileInputStream fis = new FileInputStream(file.getAbsolutePath());
                //目标路径,定义目标路径的盘符,和要复制的文件路径和文件名
                //FileOutputStream fos = new FileOutputStream("F:" + s);
                FileOutputStream fos = new FileOutputStream(sb.toString());

                //复制文件操作
                int len;
                byte[] bytes = new byte[1024 * 8];
                while ((len = fis.read(bytes)) != -1) {
                    fos.write(bytes, 0, len);
                    fos.flush();
                }

                fos.close();
                fis.close();
            } else {

                StringBuilder sb = new StringBuilder(destPath.getAbsolutePath());
                sb.append("\\").append(name1).append("\\").append(file.getName());

                
                //创建文件夹路径
                //File file1 = new File("F:" + name1);
                File file1 = new File(sb.toString());
                file1.mkdirs();
                //System.out.println(name);
                method(file, file1);
            }
        }
    }
}

再为大家补充一段代码:Java递归复制多级目录及文件,感谢原作者的分享


import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;



public class C1 {
public static void main(String[] args) throws IOException {
    File srcFolder = new File("D:\\1");
    File dstFolder = new File("F:\\新建文件夹");
    judge(srcFolder,dstFolder);
}

private static void judge(File srcFolder,File dstFolder) throws IOException {
    if(srcFolder.isDirectory()){
        File newFolder = new File(dstFolder,srcFolder.getName());
        newFolder.mkdir();

        File[] fileArr = srcFolder.listFiles();
        for(File f:fileArr){
            judge(f, newFolder);

        }
    }else{
        File newFile = new File(dstFolder,srcFolder.getName());
//      System.out.println(newFile);
        copyFile(srcFolder,newFile);

    }

}

private static void copyFile(File srcFolder, File newFile) throws IOException {
    // TODO Auto-generated method stub

    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
            srcFolder));
    BufferedOutputStream bos = new BufferedOutputStream(
            new FileOutputStream(newFile));

    byte[] bys = new byte[1024];
    int len = 0;
    while ((len = bis.read(bys)) != -1) {
        bos.write(bys, 0, len);

    }

    bos.close();
    bis.close();
}


}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持编程网。

--结束END--

本文标题: java实现系统多级文件夹复制

本文链接: https://www.lsjlt.com/news/132236.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作