zoukankan      html  css  js  c++  java
  • SequenceInputStream

    //合并多个流
        public static void merge3() throws IOException{
            File file1=new File("E:\a.txt");
            File file2=new File("E:\b.txt");
            File file3=new File("E:\c.txt");
            FileOutputStream fileOutputStream=new FileOutputStream("E:\d.txt");
            FileInputStream file1InputStream=new FileInputStream(file1);
            FileInputStream file2InputStream=new FileInputStream(file2);
            FileInputStream file3InputStream=new FileInputStream(file3);
            Vector<FileInputStream> vector=new Vector<FileInputStream>();
            vector.add(file1InputStream);
            vector.add(file2InputStream);
            vector.add(file3InputStream);
            Enumeration<FileInputStream> e=vector.elements();
            SequenceInputStream sequenceInputStream=new SequenceInputStream(e);
            byte[] buf= new byte[1024];
            int length=0;
            while((length=sequenceInputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,length);
            }
            sequenceInputStream.close();
            fileOutputStream.close();
            
        }
        
        //借助SequenceInputStream合并
        public static void merge2() throws IOException{
            File file1=new File("E:\a.txt");
            File file2=new File("E:\b.txt");
            File outFile=new File("E:\c.txt");
            FileInputStream file1InputStream=new FileInputStream(file1);
            FileInputStream file2InputStream=new FileInputStream(file2);
            FileOutputStream fileOutputStream=new FileOutputStream(outFile);
            SequenceInputStream sequenceInputStream=new SequenceInputStream(file1InputStream, file2InputStream);
            byte[] buf= new byte[1024];
            int length=0;
            while((length=sequenceInputStream.read(buf))!=-1){
                fileOutputStream.write(buf,0,length);
            }
            sequenceInputStream.close();
            fileOutputStream.close();
        
        }
        
        
        //借助数组进行合并
        public static void merge1() throws IOException{
            File file1=new File("E:\a.txt");
            File file2=new File("E:\b.txt");
            File outFile=new File("E:\c.txt");
            FileInputStream file1InputStream=new FileInputStream(file1);
            FileInputStream file2InputStream=new FileInputStream(file2);
            FileOutputStream fileOutputStream=new FileOutputStream(outFile);
            
            ArrayList<FileInputStream> list=new ArrayList<FileInputStream>();
            list.add(file1InputStream);
            list.add(file2InputStream);
            byte[] buf=new byte[1024];
            int length=0;
            for(int i=0;i<list.size();i++){
                FileInputStream fileInputStream=list.get(i);
                while((length=fileInputStream.read(buf))!=-1){
                    fileOutputStream.write(buf,0,length);
                }
                fileInputStream.close();
            }
            fileOutputStream.close();
        }
  • 相关阅读:
    TCP通信练习1(服务器给出反馈)
    TCP通信程序
    UDP通信程序
    网络编程三素概述
    wait & sleep
    多线程
    图像和音频格式解析一览
    【数学建模】matlab笔记
    【2018/7/16】神经网络BP算法和matlab入门计划
    【2018/7/15】神经网络BP算法和matlab入门计划
  • 原文地址:https://www.cnblogs.com/lyjs/p/5005074.html
Copyright © 2011-2022 走看看