以下是使用字节输入流和输出流拷贝文件夹中所有视频文件到另一个文件夹的示例代码:

import java.io.*;

public class FileCopyExample {
    public static void main(String[] args) {
        // 源文件夹路径
        String sourceFolderPath = "path_to_source_folder";
        // 目标文件夹路径
        String destinationFolderPath = "path_to_destination_folder";

        // 创建目标文件夹
        File destinationFolder = new File(destinationFolderPath);
        if (!destinationFolder.exists()) {
            destinationFolder.mkdirs();
        }

        // 获取源文件夹中所有文件
        File sourceFolder = new File(sourceFolderPath);
        File[] files = sourceFolder.listFiles();

        // 遍历源文件夹中的文件
        for (File file : files) {
            if (file.isFile()) {
                String fileName = file.getName();
                // 判断文件是否为视频文件
                if (fileName.endsWith(".mp4") || fileName.endsWith(".avi") || fileName.endsWith(".mov")) {
                    try {
                        // 创建字节输入流
                        FileInputStream fis = new FileInputStream(file);
                        // 创建字节输出流
                        FileOutputStream fos = new FileOutputStream(new File(destinationFolder, fileName));

                        // 读取源文件并写入目标文件
                        byte[] buffer = new byte[1024];
                        int length;
                        while ((length = fis.read(buffer)) > 0) {
                            fos.write(buffer, 0, length);
                        }

                        // 关闭流
                        fis.close();
                        fos.close();

                        System.out.println("文件 " + fileName + " 已成功拷贝到目标文件夹。");
                    } catch (IOException e) {
                        System.out.println("拷贝文件时发生错误。");
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

请将代码中的"path_to_source_folder"替换为源文件夹的路径,将"path_to_destination_folder"替换为目标文件夹的路径。代码中只拷贝扩展名为.mp4.avi.mov的视频文件,如果需要拷贝其他类型的视频文件,请将条件相应修改

拷贝文件下的所有视频到另一个文件夹用到字节输入流和输出流 java

原文地址: https://www.cveoy.top/t/topic/hETK 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录