• 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();
        }
  • 相关阅读:
    【转载】动态加载wpf控件主题样式资源
    paip.批处理清理java项目冗余jar的方法
    paip.java OutOfMemoryError 解决方法o33
    paip.java win程序迁移linux的最佳实践
    paip.java c# .net php python调用c++ c dll so windows api 总结
    paip.提高效率微信 手机app快速开发平台—微网络撬动大市场
    paip.Log4j配置不起作用的解决
    paip.获取地理位置根据Ip
    paip.java 开发中web server的选择jboss resin tomcat比较..
    paip.提升安全性Des加密 java php python的实现总结
  • 原文地址:https://www.cnblogs.com/lyjs/p/5005074.html
Copyright © 2020-2023  润新知