import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;public class VideoCopier {public static void main(String[] args) {// 指定源文件夹路径和目标文件夹路径String sourceFolderPath = "path/to/source/folder";String destinationFolderPath = "path/to/destination/folder";// 调用方法复制视频文件copyVideos(sourceFolderPath, destinationFolderPath);}private static void copyVideos(String sourceFolderPath, String destinationFolderPath) {// 创建源文件夹对象File sourceFolder = new File(sourceFolderPath);// 获取源文件夹下的所有文件和子文件夹File[] files = sourceFolder.listFiles();if (files != null) {for (File file : files) {if (file.isDirectory()) {// 如果是子文件夹,则递归调用该方法处理子文件夹copyVideos(file.getAbsolutePath(), destinationFolderPath);} else {// 如果是视频文件,则复制到目标文件夹中if (isVideoFile(file)) {try {Files.copy(file.toPath(), new File(destinationFolderPath, file.getName()).toPath(),StandardCopyOption.REPLACE_EXISTING);System.out.println("成功复制视频:" + file.getName());} catch (IOException e) {e.printStackTrace();}}}}}}private static boolean isVideoFile(File file) {// 判断是否为视频文件,这里简单判断后缀名为常见视频格式即可,你可以根据实际需求进行修改String fileName = file.getName();String extension = fileName.substring(fileName.lastIndexOf(".") + 1).toLowerCase();return extension.equals("mp4") || extension.equals("avi") || extension.equals("mov");}
}
实现效果: