• 文件夹复制功能的实现


    2.9 文件夹的复制

    package file;
    
    import java.io.*;
    
    public class Demo4 {
     /*
     5.将C盘中的a.txt全部剪切到D盘中
     已升级,可复制文件夹和文件
     */
     public static void main(String[] args) throws IOException {
         File fileStart = new File("E:\\java课程及工具\\课程\\Java_QF\\软件\\java编程工具\\ideaIU-2021.3.3.exe");
         File fileEnd = new File("C:\\Users\\86178\\Desktop\\测试");
         Long start = System.currentTimeMillis();
         creationDirectory(fileStart , fileEnd);
         Long end = System.currentTimeMillis();
         System.out.println((end - start)/1000);
     }
    
     //先保证所有的文件夹会通过递归的方式创建
     private static void creationDirectory(File fileStart, File fileEnd) throws IOException {
         if (fileStart.isDirectory()){
             /*
             使用新的file对象,避免地址传递导致数据异常
             获取当前文件夹的绝对路径,并创建
             */
             File directoryEnd = new File(fileEnd , fileStart.getName());
             directoryEnd.mkdir();
             //获取文件夹内的所有文件对象,并判断是否有文件夹
             File[] files = fileStart.listFiles();
             for (File file : files) {
                 File directoryName = new File(fileStart.getAbsolutePath() , file.getName());
                 if (file.isDirectory()){
                     creationDirectory(directoryName , directoryEnd);
                 }else{
                     cutFile(directoryName , directoryEnd);
                 }
             }
         }else{
             cutFile(fileStart , fileEnd);
         }
     }
    
     //文件的IO字节流
     private static void cutFile(File fileStart, File fileEnd) throws IOException {
         fileEnd = new File(fileEnd , fileStart.getName());
         OutputStream ops = new FileOutputStream(fileEnd);
         InputStream ips = new FileInputStream(fileStart);
    //        copyFile_1(ops , ips);
         copyFile_2(ops , ips);
         ops.close();
         ips.close();
     }
    
     //高效流
     private static void copyFile_2(OutputStream ops, InputStream ips) throws IOException {
         BufferedInputStream bis = new BufferedInputStream(ips);
         BufferedOutputStream bos = new BufferedOutputStream(ops);
         byte[] bs = new byte[1024];
         int len;
         while ((len = bis.read(bs)) != -1){
             bos.write(bs , 0 , len);
         }
         bos.close();
         bis.close();
     }
    
     //将复制的方法单独封装出来
     private static void copyFile_1(OutputStream ops, InputStream ips) throws IOException {
         byte[] bs = new byte[1024];
         int len;
         while ((len = ips.read(bs)) != -1){
             ops.write(bs , 0 , len);
         }
     }
    }
    
    • 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
  • 相关阅读:
    Redis_AOF
    ubuntu18.04.1LTS 编译安装ffmpeg详解
    自己动手从零写桌面操作系统GrapeOS系列教程——16.封装打印字符串函数
    【博学谷学习记录】超强总结,用心分享|架构师-Netty核心组件
    6年前的麒麟980依旧可以再战
    python爬虫hook定位技巧、反调试技巧、常用辅助工具
    C++ 引用
    2023/9/17总结
    分享一款开源的QT的串口示波器
    语音芯片的“等级”之分
  • 原文地址:https://blog.csdn.net/W_A_Hao/article/details/136171719