Java复制二进制文件

常用方法使用高效缓冲字节流

 
BufferedInputStream和BufferedOutputStream

import java.io.*;

public class BufferCopy {

    public static void main(String[] args) {
        //定义一个高效缓存字节流
        BufferedInputStream in = null;
        BufferedOutputStream out = null;
        try {
            //创建一个高效缓存字节流对象
            in = new BufferedInputStream(new FileInputStream("C:/yonige.png"));
            out = new BufferedOutputStream(new FileOutputStream("D:/ComingSpring.png"));
            //定义一个字节数组
            byte[] bs = new byte[1024];
            //定义一个标志
            int len = -1;
            while((len = in.read(bs)) != -1){
                out.write(bs, 0, len);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if(out != null){
                    out.close();
                }
                if(in != null){
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

猜你喜欢

转载自www.cnblogs.com/seviyan/p/11657925.html
今日推荐