• 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();
        }
  • 相关阅读:
    添加AdMob的Native广告
    androidx.recyclerview.widget.RecyclerView的使用方法
    androidx.recyclerview.widget.RecyclerView 使用记录
    ViewPager使用
    小程序加载云端数据库中的第二页数据,前端如何动态显示?
    小程序页面跳转传递参数
    小程序云端函数 where遇到的问题
    Python 加载包含多个JSON对象的JSON文件
    如何把一个目录下的中文名字的文件全部变成拼音命名的文件?
    小技巧--解决eclipse导入的jar文件后,无法使用默认包中的方法问题
  • 原文地址:https://www.cnblogs.com/lyjs/p/5005074.html
Copyright © 2020-2023  润新知