字节输入输出流

package com.sxt.copy1;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/*
 * 字节输入输出流
 * InputStream
 * OutputStream
 * 实现Copy文件操作
 */
public class TestBCopy {
    public static void main(String[] args){
        InputStream is = null;
        OutputStream os = null;
        try {
            //输入流
            is = new FileInputStream("F:\01_面向对象设计思想_重要_1.avi");
            //输出流
            os = new FileOutputStream("G:\CopyDest.avi");
            //新建字节数组
            byte[] b = new byte[1024];
            //第一步:读取文件到程序
            int len = 0;
            while((len = is.read(b)) != -1){//读取文件到数组 同时返回数据长度
                System.out.println(len);
                os.write(b, 0, len);//第二步:再从程序读到文件   System.arraycopy
            }
            System.out.println("Copy操作完成!");
            
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(is != null){//确定流打开再关闭
                try {
                    is.close();//关闭流
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if(os != null){
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
原文地址:https://www.cnblogs.com/qingfengzhuimeng/p/6764146.html