栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

JAVA复制文件和目录

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

JAVA复制文件和目录

 用Java复制目录和文件,代码如下:

ackage com.work.ioliu;


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

public class Tests {

    static void close(AutoCloseable... acs) {
        for (AutoCloseable ac : acs) {
            if (ac != null) {
                try {
                    ac.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }

    static float copy(String srcPath, String dstPath) {
        //单个文件拷贝
        final int M = 1024 * 1024;
        final int M16 = 16 * M;
        final long FILE_SIZE = new File(srcPath).length();
        int BUFFER_SIZE = (int) FILE_SIZE / 8;
        BUFFER_SIZE = BUFFER_SIZE > M16 ? M16 : BUFFER_SIZE<=0?M:BUFFER_SIZE;

        float total = 0;
        BufferedInputStream bis = null;
        BufferedOutputStream bos = null;
        try {
            bis = new BufferedInputStream(new java.io.FileInputStream(srcPath));
            bos = new BufferedOutputStream(new java.io.FileOutputStream(dstPath));
            byte[] bytes = new byte[2 * M];
            int read = -1;

            long begin = System.currentTimeMillis();
            while (-1 != (read = bis.read(bytes))) {
                bos.write(bytes, 0, read);
                total += read;
                System.out.println(total / FILE_SIZE);
            }
            bos.flush();
            System.out.println((System.currentTimeMillis() - begin) / 1000.0);
        } catch (FileNotFoundException e) {
            e.printStackTrace();//找不到路径
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            close(bos, bis);
        }
        return total / FILE_SIZE;
    }

    static void dirCopy(File srcDir, File dstDir) {
        if (!srcDir.exists()) return;
        File[] files = srcDir.listFiles();

//      if (files==null){
//          return;
//      }
        for (File file : files) {
            String dst = (dstDir.getAbsolutePath().endsWith("\") ? dstDir.getAbsolutePath() : (dstDir.getAbsolutePath() + "\")) + file.getName();
            File newFile = new File(dst);
            if (file.isFile()) {
                float copy = copy(file.getPath(), dst);
                if (copy == 1) {
                    System.out.println("succeed");

                } else {
                    System.out.println("failed");
                }
            }

            if (file.isDirectory()) {
              if(!newFile.exists()){
                  newFile.mkdirs();
              }
        dirCopy(file, newFile);

    }

}

    }

    public static void main(String[] args) {
        final String SRCPATH = "C:\Users\18915\Desktop\iff";
        final String DSTPATH = "C:\Users\18915\Desktop\ff";
        File file = new File(SRCPATH);
        File file1 = new File(DSTPATH);
        dirCopy(file, file1);
    }

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/995773.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号