SequenceInputStream:序列流,对多个流进行合并。
SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
序列流的使用:
可以将多个流串联到一起 ,然后就通过串联一个一来读取流中的数据。
--- SequenceInputStream 只能操作输入流。
方式二和方式三即为SequenceInputStream的用法:
package com.beiwo.io; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.SequenceInputStream; import java.util.ArrayList; import java.util.Enumeration; import java.util.List; import java.util.Vector; public class demo6 { /**
*
*/ public static void main(String[] args) throws Exception { // TODO 自动生成的方法存根 testMerge1(); testMerge2(); testMerge3(); } // 方式三:可以同时操作多个文件夹 public static void testMerge3() throws Exception { // 获取目标文件、 File file1 = new File("C:\Users\cdlx2016\Desktop\2\1.txt"); File file2 = new File("C:\Users\cdlx2016\Desktop\2\2.txt"); File file3 = new File("C:\Users\cdlx2016\Desktop\2\3.txt"); File file4 = new File("C:\Users\cdlx2016\Desktop\2\4.txt"); // 如果没有目标文件就创建 if (!file1.exists()) { file1.createNewFile(); } if (!file2.exists()) { file2.createNewFile(); } if (!file3.exists()) { file3.createNewFile(); } if (!file4.exists()) { file4.createNewFile(); } // 建立通道 FileInputStream fileInputStream1 = new FileInputStream(file1); FileInputStream fileInputStream2 = new FileInputStream(file2); FileInputStream fileInputStream3 = new FileInputStream(file3); FileOutputStream fileOutputStream = new FileOutputStream(file4); // 创建一个vector集合对象 Vector<FileInputStream> vector = new Vector<FileInputStream>(); // 添加 vector.add(fileInputStream1); vector.add(fileInputStream2); vector.add(fileInputStream3); // 获取迭代器 Enumeration<FileInputStream> enumeration = vector.elements(); // 通过序列化将三个流串起来 SequenceInputStream sequenceInputStream = new SequenceInputStream(enumeration); // 创建字节数组 byte[] b = new byte[1024]; // 读取数据 int length = 0; while ((length = sequenceInputStream.read(b)) != -1) { // 写入数据 fileOutputStream.write(b, 0, length); } // 关闭流 fileOutputStream.close(); sequenceInputStream.close(); } // 方式二:简化方式一的操作 public static void testMerge2() throws Exception { // 获取目标文件、 File file1 = new File("C:\Users\cdlx2016\Desktop\2\1.txt"); File file2 = new File("C:\Users\cdlx2016\Desktop\2\2.txt"); File file3 = new File("C:\Users\cdlx2016\Desktop\2\3.txt"); // 如果没有目标文件就创建 if (!file1.exists()) { file1.createNewFile(); } if (!file2.exists()) { file2.createNewFile(); } if (!file3.exists()) { file3.createNewFile(); } // 建立通道 FileInputStream fileInputStream1 = new FileInputStream(file1); FileInputStream fileInputStream2 = new FileInputStream(file2); FileOutputStream fileOutputStream = new FileOutputStream(file3); // 建立序列流 SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1, fileInputStream2); byte[] b = new byte[1024]; // 读取数据 int length = 0; while ((length = sequenceInputStream.read(b)) != -1) { // 写入数据 fileOutputStream.write(b, 0, length); } // 关闭流 fileOutputStream.close(); sequenceInputStream.close(); } // 方法一:操作太复杂了 public static void testMerge1() throws Exception { // 获取目标文件、 File file1 = new File("C:\Users\cdlx2016\Desktop\2\1.txt"); File file2 = new File("C:\Users\cdlx2016\Desktop\2\2.txt"); File file3 = new File("C:\Users\cdlx2016\Desktop\2\3.txt"); // 如果没有目标文件就创建 if (!file1.exists()) { file1.createNewFile(); } if (!file2.exists()) { file2.createNewFile(); } if (!file3.exists()) { file3.createNewFile(); } // 建立通道 FileInputStream fileInputStream1 = new FileInputStream(file1); FileInputStream fileInputStream2 = new FileInputStream(file2); FileOutputStream fileOutputStream = new FileOutputStream(file3); // 用集合来存输入流 List<FileInputStream> list = new ArrayList<FileInputStream>(); // 添加元素 list.add(fileInputStream1); list.add(fileInputStream2); // 边读边写数据 byte[] b = new byte[1024]; int length = 0; for (int i = 0; i < list.size(); i++) { while ((length = list.get(i).read(b)) != -1) { // 写入数据 fileOutputStream.write(b, 0, length); } } // 关闭流 先开后关 fileOutputStream.close(); fileInputStream2.close(); fileInputStream1.close(); } }