使用对接字节数组流进行文件复制

·InputStream

\quad 此抽象类是表示字节输入流的所有类的父类。InputSteam是一个抽象类,它不可以实例化。 数据的读取需要由它的子类来实现。根据节点的不同,它派生了不同的节点流子类,常用的如FileInputStream,ByteArrayInputStream等。
\quad 继承自InputSteam的流都是用于向程序中输入数据,且数据的单位为字节(8 bit)。

·OutputStream

\quad 此抽象类是表示字节输出流的所有类的父类。输出流接收输出字节并将这些字节发送到某个目的地。
以上流在使用完毕后要执行close来关闭流并释放与该流相关联的所有系统资源。注意,JVM不能直接对接文件,只能向操作系统申请。即JVM告知操作系统,由操作系统释放。

·ByteArrayInputStream / ByteArrayOutputStream

\quad 以字节为单位直接操作“字节数组对象”。由于操作对象为JVM可以直接管理的内存,由垃圾回收机制管理,所以无需关闭流(重写的close方法为空方法)。

import java.io.*;
public class IOTest05 {
    public static void main(String[] args) {
        byte[] data = fileToByteArray("test.bmp");
        System.out.println(data.length);
        ByteArrayTofile(data, "test-byte.bmp");
    }
    /**
     * 1、文件到字节数组
     *  1)、文件到程序
     *  2)、程序到字节数组
     */
    public static byte[] fileToByteArray(String srcPath) {
        //1、创建源
        File src = new File(srcPath);
        //2、选择流
        InputStream is = null;
        ByteArrayOutputStream baos = null;   //不能用OutputStream,这里需要用到子类的新方法

        try {
            is = new FileInputStream(src);
            baos = new ByteArrayOutputStream();

            //3、操作
            byte[] flush = new byte[1024*10];     //缓冲容器
            int len = -1;
            while((len = is.read(flush)) != -1)   //从源文件到缓冲容器
                baos.write(flush,0, len);         //从缓冲容器到目标字节数组
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if(is != null) {
                try {
                    is.close();           //InputStream对象需要关闭
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            return baos.toByteArray();     //返回目标字节数组
        }
    }

    /**
     * 2、字节数组到文件
     *  1)、字节数组到程序
     *  2)、程序到文件
     */
    public static void ByteArrayTofile(byte[] src, String desPath) {
        //1、创建源
        File des = new File(desPath);
        //2、选择流
        InputStream is = null;
        OutputStream os = null;

        try {
            is = new ByteArrayInputStream(src);
            os = new FileOutputStream(des);
            //3、操作
            byte[] flush = new byte[5];
            int len = -1;
            while((len = is.read(flush)) != -1)  //从源字节数组到缓冲容器
                os.write(flush,0, len);          //从缓冲容器到目标文件
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //4、释放系统资源
            if(os != null) {
                try {
                    os.close();          //OutputStream对象需要关闭
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值