服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - java实现系统多级文件夹复制

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

2021-11-12 14:53Jae_Du Java教程

这篇文章主要为大家详细介绍了java实现系统多级文件夹复制,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
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());
 
                /*String name1 = file.getName();
                //获取文件夹的路径
                String name = file.getPath();
                //获取盘符:后的文件夹路径
                String s = name.split(":")[1];*/
                //创建文件夹路径
                //File file1 = new File("F:" + name1);
                File file1 = new File(sb.toString());
                file1.mkdirs();
                //System.out.println(name);
                method(file, file1);
            }
        }
    }
}

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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
/*
 * 需求:复制多级文件夹
 *
 * 数据源:D:\\1
 * 目的地:F:\\新建文件夹
 *
 * 分析:
 *      A:封装数据源File
 *      B:封装目的地File
 *      C:判断该File是文件夹还是文件
 *          a:是文件夹
 *              就在目的地目录下创建该文件夹
 *              获取该File对象下的所有文件或者文件夹File对象
 *              遍历得到每一个File对象
 *              回到C
 *          b:是文件
 *              就复制(字节流)
 */
 
 
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();
}
 
 
}

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

原文链接:https://blog.csdn.net/weixin_41869890/article/details/90291932

延伸 · 阅读

精彩推荐