//合并多个流 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(); }