Java获取指定文件夹下目录下所有视频并复制到另一个地方

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");
    }
}

实现效果:

猜你喜欢

转载自blog.csdn.net/weixin_39709134/article/details/132165457