以下是一个使用字节输入流和输出流拷贝文件的示例代码:

import java.io.*;

public class FileCopyExample {
    public static void main(String[] args) throws IOException {
        // 源文件夹路径
        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()) {
                // 创建输入流和输出流
                FileInputStream inputStream = new FileInputStream(file);
                FileOutputStream outputStream = new FileOutputStream(destinationFolder.getAbsolutePath() + File.separator + file.getName());

                // 读取和写入文件内容
                int bytesRead;
                byte[] buffer = new byte[1024];
                while ((bytesRead = inputStream.read(buffer)) != -1) {
                    outputStream.write(buffer, 0, bytesRead);
                }

                // 关闭流
                inputStream.close();
                outputStream.close();
            }
        }

        System.out.println("文件拷贝完成!");
    }
}

请注意,这只是一个简单的示例代码,未进行异常处理。在实际应用中,建议添加适当的异常处理代码来处理可能出现的异常情况

拷贝文件下的所有视频到另一个文件夹用到字节输入流和输出流 不要抓取异常直接抛出异常即可java

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

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