經過RecursiveAction複製文件ide
public class MoveFiles extends RecursiveAction { private File path; /** * 源文件夾 */ static String SOURCE_URL = "G:\\workspaces2"; /** * 目標文件夾 */ static String TARGET_URL = "H:\\test"; public MoveFiles(File path) { this.path = path; } @Override protected void compute() { List<MoveFiles> subTasks = new ArrayList<>(); File[] files = path.listFiles(); if (files != null) { for (File file : files) { String absolutePath = file.getAbsolutePath(); String targetDir = absolutePath.replace(SOURCE_URL, TARGET_URL); File targetPath = new File(targetDir); if (file.isDirectory()) { targetPath.mkdirs(); subTasks.add(new MoveFiles(file)); } else { try { Files.copy(file.toPath(), targetPath.toPath()); } catch (IOException e) { e.printStackTrace(); } } } if (!subTasks.isEmpty()) { for (MoveFiles subTask : invokeAll(subTasks)) { subTask.join(); } } } } public static void main(String[] args) { try { ForkJoinPool pool = new ForkJoinPool(); MoveFiles task = new MoveFiles(new File(SOURCE_URL)); pool.execute(task); long start = System.currentTimeMillis(); System.out.println("任務開始"); task.join(); long end = System.currentTimeMillis(); System.out.println("任務結束,耗時:" + (end - start) / 1000); } catch (Exception e) { e.printStackTrace(); } } }