JAVA字节缓冲流代码实现所有类型文件的复制

Posted private-mingmie

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA字节缓冲流代码实现所有类型文件的复制相关的知识,希望对你有一定的参考价值。

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyAll {
    public static void main(String[] args) {
        //原路径
        String oldFile = "";
        //新路径
        String newFile = "";
        //调用拷贝所有文件的方法
        copyAll(oldFile,newFile);
    }
    //定义一个拷贝文件的方法,oldFile-->原路径 newFile-->新路径
    public static void copyAll(String oldFile,String newFile) {
        try (//创建字节输入缓冲流对象
            BufferedInputStream bis = new BufferedInputStream(new FileInputStream(new File(oldFile)));
            //创建字节输出缓冲流对象
            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(new File(newFile)))
                ){
            //复制,一边读一边写
            byte[] by = new byte[1024];
            int len;
            while((len = bis.read(by)) != -1) {
                bos.write(by, 0, len);
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            
        }
    }
}

以上是关于JAVA字节缓冲流代码实现所有类型文件的复制的主要内容,如果未能解决你的问题,请参考以下文章