JAVA基础(序列流)

1,什么是序列流

  • 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推

 

2,使用方式

  • 整合两个: SequenceInputStream(InputStream, InputStream)

 

           FileInputStream fis1 = new FileInputStream("a.txt");            //创建输入流对象,关联a.txt

            FileInputStream fis2 = new FileInputStream("b.txt");            //创建输入流对象,关联b.txt

            SequenceInputStream sis = new SequenceInputStream(fis1, fis2);    //将两个流整合成一个流

            FileOutputStream fos = new FileOutputStream("c.txt");            //创建输出流对象,关联c.txt

            

            int b;

            while((b = sis.read()) != -1) {                                    //用整合后的读

                fos.write(b);                                                //写到指定文件上

            }

            

            sis.close();

            fos.close();

 

3,序列流整合多个,MP3的歌整合

  • 整合多个: SequenceInputStream(Enumeration)

        FileInputStream fis1 = new FileInputStream("a.txt");    //创建输入流对象,关联a.txt

        FileInputStream fis2 = new FileInputStream("b.txt");    //创建输入流对象,关联b.txt

        FileInputStream fis3 = new FileInputStream("c.txt");    //创建输入流对象,关联c.txt

        Vector<InputStream> v = new Vector<>();                    //创建vector集合对象

        v.add(fis1);                                                                        //将流对象添加

        v.add(fis2);

        v.add(fis3);

        Enumeration<InputStream> en = v.elements();                //将枚举中的输入流整合在一起

        SequenceInputStream sis = new SequenceInputStream(en);    //传递给SequenceInputStream构造

        FileOutputStream fos = new FileOutputStream("d.txt");

        int b;

        while((b = sis.read()) != -1) {

            fos.write(b);

        }

    

        sis.close();

        fos.close();

4,案例

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.SequenceInputStream;

import java.util.Enumeration;

import java.util.Vector;





public class Demo01_SequenceInputStream {





    /**

     * @param args

     * 整合两个输入流

     * SequenceInputStream(InputStream s1, InputStream s2)

     * 整合多个输入流

     * SequenceInputStream(Enumeration<? extends InputStream> e)

     * @throws IOException

     */

    public static void main(String[] args) throws IOException {

        //demo1();

        //demo2();

        FileInputStream fis1 = new FileInputStream("a.txt");

        FileInputStream fis2 = new FileInputStream("b.txt");

        FileInputStream fis3 = new FileInputStream("c.txt");

        

        Vector<FileInputStream> v = new Vector<>();                    //创建集合对象

        v.add(fis1);                                                //将流对象存储进来

        v.add(fis2);

        v.add(fis3);

        

        Enumeration<FileInputStream> en = v.elements();

        SequenceInputStream sis = new SequenceInputStream(en);        //将枚举中的输入流整合成一个

        FileOutputStream fos = new FileOutputStream("d.txt");

        

        int b;

        while((b = sis.read()) != -1) {

            fos.write(b);

        }

        

        sis.close();

        fos.close();

    }





    public static void demo2() throws FileNotFoundException, IOException {

        FileInputStream fis1 = new FileInputStream("a.txt");

        FileInputStream fis2 = new FileInputStream("b.txt");

        SequenceInputStream sis = new SequenceInputStream(fis1, fis2);

        FileOutputStream fos = new FileOutputStream("c.txt");

        

        int b;

        while((b = sis.read()) != -1) {

            fos.write(b);

        }

        

        sis.close();                    //sis在关闭的时候,会将构造方法中传入的流对象也都关闭

        fos.close();

    }





    public static void demo1() throws FileNotFoundException, IOException {

        FileInputStream fis1 = new FileInputStream("a.txt");        //创建字节输入流关联a.txt

        FileOutputStream fos = new FileOutputStream("c.txt");        //创建字节输出流关联c.txt

        

        int b1;

        while((b1 = fis1.read()) != -1) {                            //不断的在a.txt上读取字节

            fos.write(b1);                                            //将读到的字节写到c.txt上

        }

        fis1.close();                                                //关闭字节输入流

        

        FileInputStream fis2 = new FileInputStream("b.txt");

        int b2;

        while((b2 = fis2.read()) != -1) {

            fos.write(b2);

        }

        

        fis2.close();

        fos.close();

    }





}





 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

兴帅_

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值