当前位置: 首页 > news >正文

Java实现使用多线程,实现复制文件到另一个目录,起不一样的名字,创建100万个数据

目录

  • 1 需求
  • 2 实现

1 需求

我现在有一个300MB 的文件,想要根据这个文件,创建100万个大小一样的,名称不一样,如何实现,如何比较快点实现

2 实现

1 先准备好这个文件

在这里插入图片描述

2 准备好目录

在这里插入图片描述
3 写代码

private static void createFile(String sourceFilePath,String destinationFolderPath, int fileNumber) {File file1 = new File(sourceFilePath);Path sourcePath = Paths.get(sourceFilePath);String destinationFileName = "copy_" + UUID.randomUUID().toString() + "_" + file1.getName();Path destinationPath = Paths.get(destinationFolderPath, destinationFileName);//try {// 复制源文件到目标文件Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) {String sourceFilePath = "D:\\100w\\source\\1111111111111.HDF"; // 替换为实际的源文件路径String destinationFolderPath = "D:\\100w\\dest"; // 替换为实际的目标文件夹路径int numFiles = 1000000; // 需要创建的文件数量int numThreads = Runtime.getRuntime().availableProcessors(); // 使用可用的处理器核心数作为线程数ExecutorService executorService = Executors.newFixedThreadPool(numThreads);try {// 创建目标文件夹(如果不存在)Files.createDirectories(Paths.get(destinationFolderPath));// 循环提交文件创建任务给线程池for (int i = 100; i < numFiles; i++) {int fileNumber = i;executorService.submit(() -> createFile(sourceFilePath,destinationFolderPath, fileNumber));}executorService.shutdown();} catch (IOException e) {e.printStackTrace();}//        try {//            // 获取源文件的路径对象//            Path sourcePath = Paths.get(sourceFilePath);////            // 创建目标文件夹(如果不存在)//            Files.createDirectories(Paths.get(destinationFolderPath));////            // 循环复制文件并创建副本文件//            for (int i = 0; i < numFiles; i++) {//                // 构造目标文件的路径对象//                String destinationFileName = "copy_" + i + "_" + sourcePath.getFileName();//                Path destinationPath = Paths.get(destinationFolderPath, destinationFileName);////                // 复制源文件到目标文件//                Files.copy(sourcePath, destinationPath, StandardCopyOption.REPLACE_EXISTING);//            }////            System.out.println("文件复制完成!");//        } catch (IOException e) {//            e.printStackTrace();//        }}
http://www.lryc.cn/news/178791.html

相关文章:

  • uni-app:canvas-图形实现1
  • 【算法分析与设计】动态规划(下)
  • 计算机图像处理-均值滤波
  • FreeRTOS入门教程(空闲任务和钩子函数及任务调度算法)
  • Javascript真的是10天内做出来的吗?
  • picoctf_2018_got_shell
  • 作用域 CSS 回来了
  • 简述ceph文件储存系统
  • 计算机图像处理:椒盐噪声和高斯噪声
  • SQL SELECT 子查询与正则表达式
  • Package vips was not found in the pkg-config search path的解决方案
  • Vue封装全局SVG组件
  • 课题学习(二)----倾角和方位角的动态测量方法(基于磁场的测量系统)
  • Docker-Windows安装使用
  • 在Windows11上安装ubuntu虚拟机
  • 【微服务】spring 控制bean加载顺序使用详解
  • python-切换镜像源和使用PyCharm进行第三方开源包安装
  • tp6 + swagger 配置文档接口
  • 试图一文彻底讲清 “精准测试”
  • Visual Studio 删除行尾空格
  • LeetCode_BFS_中等_1926.迷宫中离入口最近的出口
  • 开源Windows12网页版HTML源码
  • vscode中使用指定路径下的cmake
  • 复杂度分析
  • Linux安装jrockit-jdk1.6.0_29-R28.2.0-4.1.0-linux-x64
  • 7.2 怎样定义函数
  • Chrome扩展V2到V3的变化
  • lock、tryLock、lockInterruptibly有什么区别?
  • mysql面试题5:索引、主键、唯一索引、联合索引的区别?什么情况下设置了索引但无法使用?并且举例说明
  • 数据集笔记:纽约花旗共享单车od数据