目录
- 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();// }}