IO中其他流--节点流

节点流

1、字节数组节点流
输入流:ByteArrayInputStream read(byte[] b,int off,int len) + close()
输出流:ByteArrayOutputStream write(byte[] b,int off,int len) + toByteArray()(父类没有的新方法,不要使用多态)

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
/**
 * 字节数组节点流
 * 数组的长度有限,数据量不会很大
 */
public class ByteArrayStream {
    public static void main(String[] args) throws IOException {
        read(write("传入字符"));
    }
    /**
     * 输入流操作与文件输入流操作一致
     * 读取字节数组
     * @throws IOException 
     */
    public static void read(byte[] src) throws IOException{
        //数据源(模拟其它电脑上的内存)
//      String msg = "输入流操作与文件输入流操作一致";
//      byte[] src = msg.getBytes();
        //数据源改为传入
        //选择流
        InputStream is = new BufferedInputStream(
            new ByteArrayInputStream(
                src     
            )
        );
        //操作
        byte[] flush = new byte[1024];
        int len = 0;
        while(-1 != (len=is.read(flush))){
            System.out.println(new String(flush,0,len));
        }
        //释放资源,可以不调用
        is.close();
    }

     /**
     * 输出流操作与文件输出流有些不同,有新增方法,不能使用多态
     * @throws IOException 
     */
    public static byte[] write(String msg) throws IOException{
        //目的地
        byte[] des;
        //选择流   与文件输出流的一个不同点:没有传入参数
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //操作    写出
        //String msg = "输出流操作与文件输出流有些不同,有新增方法,不能使用多态";
        byte[] info = msg.getBytes();
        baos.write(info,0,info.length);
        //获取数据
        des = baos.toByteArray();
        //释放资源
        baos.close();
        return des;
    }
}

字节数组流、与文件流对接,也就是利用节点流实现文件的拷贝

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/**
 * 1、文件 -程序->字节数组
 * 文件输入流
 * 字节数组输出流
 * 2、字节数组 -程序->文件
 * 字节数组输入流
 * 文件输出流
 */
public class ByteArrayAndFile {
    public static void main(String[] args) throws IOException {
        byte[] data = getBytesFromFile("D:/安装包/aa/bb/a.txt");
//        System.out.println(new String(data));
        toFileFromByteArray(data,"D:/安装包/aa/bb/c.txt");
    }
    /**
     * 1、文件 -程序->字节数组
     * @param srcPath
     * @return
     * @throws IOException
     */
    public static byte[] getBytesFromFile(String srcPath) throws IOException{
        //创建文件源
        File src = new File(srcPath);
        //创建字节数组目的地
        byte[] des = null;
        //选择流
        //文件输入流
        InputStream is = new BufferedInputStream(new FileInputStream(src));
        //字节数组输出流   不能使用多态
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //操作    不断读取文件,写出到字节数组流中
        byte[] flush = new byte[1024];
        int len = 0;
        while(-1 != (len=is.read(flush))){
            //写出到字节数组流中
            baos.write(flush,0,len);
        }
        baos.flush();
        //获取数据,因为inputStream里面的内容你不知道具体有多长,所以无法确定到底buffer需要多长1024也许未必够用,所以只能把buffer当做缓存,
        //每次读进一部分,在把buffer的内容,写到足够大的内存区。如果你确定你的输入流中字节数小于1024,你也可以直接返回buffer
        //ByteArrayOutputStream时将数据窜存放在管道中,但是FileOutputStream将数据写入到文件中,所以需要使用这个方法将数据取出来使用
        des = baos.toByteArray();
        //释放资源
        baos.close();
        is.close();
        return des;
    }
    /**
     * 2、字节数组 -程序->文件
     */
    public static void toFileFromByteArray(byte[] src,String desPath) throws IOException{
        //创建源
        //目的地
        File des = new File(desPath);
        //选择流
        //字节数组输入流
        InputStream is = new BufferedInputStream(new ByteArrayInputStream(src));
        //文件输出流
        OutputStream os = new BufferedOutputStream(new FileOutputStream(des));
        //操作    不断读取字节数组
        byte[] flush = new byte[1024];
        int len = 0;
        while(-1 != (len=is.read(flush))){
            //写出到文件中
            os.write(flush, 0, len);
        }
        os.flush();
        //释放资源
        is.close();
        os.close();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值