SequenceInputStream:序列流,对多个流进行合并。

SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。

序列流的使用:

可以将多个流串联到一起  ,然后就通过串联一个一来读取流中的数据。

--- SequenceInputStream   只能操作输入流。

方式二和方式三即为SequenceInputStream的用法:

  1. package com.beiwo.io;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileOutputStream;
  6. import java.io.SequenceInputStream;
  7. import java.util.ArrayList;
  8. import java.util.Enumeration;
  9. import java.util.List;
  10. import java.util.Vector;
  11.  
  12. public class demo6 {
  13.   /**
      * 
      */
  14. public static void main(String[] args) throws Exception {
  15. // TODO 自动生成的方法存根
  16. testMerge1();
  17. testMerge2();
  18. testMerge3();
  19. }
  20. // 方式三:可以同时操作多个文件夹
  21. public static void testMerge3() throws Exception {
  22. // 获取目标文件、
  23. File file1 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\1.txt");
  24. File file2 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\2.txt");
  25. File file3 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\3.txt");
  26. File file4 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\4.txt");
  27. // 如果没有目标文件就创建
  28. if (!file1.exists()) {
  29. file1.createNewFile();
  30. }
  31. if (!file2.exists()) {
  32. file2.createNewFile();
  33. }
  34. if (!file3.exists()) {
  35. file3.createNewFile();
  36. }
  37. if (!file4.exists()) {
  38. file4.createNewFile();
  39. }
  40. // 建立通道
  41. FileInputStream fileInputStream1 = new FileInputStream(file1);
  42. FileInputStream fileInputStream2 = new FileInputStream(file2);
  43. FileInputStream fileInputStream3 = new FileInputStream(file3);
  44. FileOutputStream fileOutputStream = new FileOutputStream(file4);
  45. // 创建一个vector集合对象
  46. Vector<FileInputStream> vector = new Vector<FileInputStream>();
  47. // 添加
  48. vector.add(fileInputStream1);
  49. vector.add(fileInputStream2);
  50. vector.add(fileInputStream3);
  51. // 获取迭代器
  52. Enumeration<FileInputStream> enumeration = vector.elements();
  53. // 通过序列化将三个流串起来
  54. SequenceInputStream sequenceInputStream = new SequenceInputStream(enumeration);
  55. // 创建字节数组
  56. byte[] b = new byte[1024];
  57. // 读取数据
  58. int length = 0;
  59. while ((length = sequenceInputStream.read(b)) != -1) {
  60. // 写入数据
  61. fileOutputStream.write(b, 0, length);
  62. }
  63. // 关闭流
  64. fileOutputStream.close();
  65. sequenceInputStream.close();
  66. }
  67.  
  68. // 方式二:简化方式一的操作
  69. public static void testMerge2() throws Exception {
  70. // 获取目标文件、
  71. File file1 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\1.txt");
  72. File file2 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\2.txt");
  73. File file3 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\3.txt");
  74. // 如果没有目标文件就创建
  75. if (!file1.exists()) {
  76. file1.createNewFile();
  77. }
  78. if (!file2.exists()) {
  79. file2.createNewFile();
  80. }
  81. if (!file3.exists()) {
  82. file3.createNewFile();
  83. }
  84. // 建立通道
  85. FileInputStream fileInputStream1 = new FileInputStream(file1);
  86. FileInputStream fileInputStream2 = new FileInputStream(file2);
  87. FileOutputStream fileOutputStream = new FileOutputStream(file3);
  88. // 建立序列流
  89. SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1, fileInputStream2);
  90. byte[] b = new byte[1024];
  91. // 读取数据
  92. int length = 0;
  93. while ((length = sequenceInputStream.read(b)) != -1) {
  94. // 写入数据
  95. fileOutputStream.write(b, 0, length);
  96. }
  97. // 关闭流
  98. fileOutputStream.close();
  99. sequenceInputStream.close();
  100. }
  101.  
  102. // 方法一:操作太复杂了
  103. public static void testMerge1() throws Exception {
  104. // 获取目标文件、
  105. File file1 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\1.txt");
  106. File file2 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\2.txt");
  107. File file3 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\3.txt");
  108. // 如果没有目标文件就创建
  109. if (!file1.exists()) {
  110. file1.createNewFile();
  111. }
  112. if (!file2.exists()) {
  113. file2.createNewFile();
  114. }
  115. if (!file3.exists()) {
  116. file3.createNewFile();
  117. }
  118. // 建立通道
  119. FileInputStream fileInputStream1 = new FileInputStream(file1);
  120. FileInputStream fileInputStream2 = new FileInputStream(file2);
  121. FileOutputStream fileOutputStream = new FileOutputStream(file3);
  122. // 用集合来存输入流
  123. List<FileInputStream> list = new ArrayList<FileInputStream>();
  124. // 添加元素
  125. list.add(fileInputStream1);
  126. list.add(fileInputStream2);
  127. // 边读边写数据
  128. byte[] b = new byte[1024];
  129. int length = 0;
  130. for (int i = 0; i < list.size(); i++) {
  131. while ((length = list.get(i).read(b)) != -1) {
  132. // 写入数据
  133. fileOutputStream.write(b, 0, length);
  134. }
  135. }
  136. // 关闭流 先开后关
  137. fileOutputStream.close();
  138. fileInputStream2.close();
  139. fileInputStream1.close();
  140. }
  141.  
  142. }

序列流 SequenceInputStream的更多相关文章

  1. IO流(五)__文件的递归、Properties、打印流PrintStream与PrintWriter、序列流SequenceInputStream

    一.文件的遍历 1.需求:对指定目录进行所有的内容的列出(包含子目录的内容)-文件的深度遍历 思想:递归的思想,在递归的时候要记住递归的层次. public class FileTest { publ ...

  2. Java之序列流SequenceInputStream

    序列流:作用就是将多个读取流合并成一个读取流,实现数据的合并 序列流表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到文件的末尾,接着从第二个输入流读取,以此类推:这样 ...

  3. JAVA学习第五十四课 — IO流(八)打印流 &amp; 序列流

    一.综合练习-文件清单列表 获取指定文件夹下,指定扩展名的文件(含子文件夹),并将这些文件的绝对路径写到一个文本文件里.也就是建立一个指定扩展名的文件列表 1.深度遍历 2.过滤器->容器 3. ...

  4. (20)IO流之SequenceInputStream 序列流

    序列流,对多个流进行合并. SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直 ...

  5. IO流(SequenceInputStream序列流--文件拆分与合并)

    一.文件拆分 1.将一个文件按照规定的大小进行分割成多个文件并将被分割详细信息保存至配置信息中 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载,属性列表 ...

  6. Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)

    一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...

  7. java IO之 序列流 集合对象Properties 打印流 流对象

    序列流 也称为合并流. SequenceInputStream 序列流,对多个流进行合并. SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从 第一个输入 ...

  8. Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)

    第一讲     File类 一.概述 1.File类:文件和目录路径名的抽象表现形式 2.特点: 1)用来将文件或文件夹封装成对象 2)方便于对文件与文件夹的属性信息进行操作 3)File类的实例是不 ...

  9. IO流_SequenceInputStream(序列流)

    SequenceInputStream(序列流):就是将多个流合成一个有序的流 需求:将三个文件中的数据合并到一个文件中 import java.io.FileInputStream; import ...

随机推荐

  1. jquery numberbox赋值

    numberbox不能使用$('#id').val( '');只能使用$('#id').numberbox('setValue','');

  2. UIScrollView的基本使用

    UIScrollView的用法很简单 将需要展示的内容添加到UIScrollView中 设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸,也就是告诉 ...

  3. getcwd()和dirname(__FILE__)的区别

    我个人理解:getcwd()会随着包含文件的改变而改变,而dirname(__FILE__)不会.即 getcwd() 表示获取当前执行文件的物理路径. 如 getcwd()显示: /www/proj ...

  4. C/C++内存、指针问题

    转 http://wenku.baidu.com/link?url=tN9Fac-XyB2F7V7xwYcRclu464G2c8ybYMBxNXbBGQJXEEy0vJxTOzcAeVrFrqYLfj ...

  5. poj 1733

    这题离散化+并查集,没看出关dp什么事.(那他为什么放到dp里面) 用Si记录前i项的和.拆成两个点,i*2表示与第i个相同,i*2+1表示与第i个不同.用并查集判断.区间[a,b]就可以看成Sb-S ...

  6. jQuery UI Datepicker

    http://www.runoob.com/try/try.php?filename=jqueryui-example-datepicker-dropdown-month-year <!doct ...

  7. this a a mark down test

    *** this is title ''' code '''

  8. ASP.NET知识总结(3.HTTP协议详解)

    引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...

  9. 【leetcode】Perfect Squares (#279)

    Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...

  10. Oracle资源管理器(二)-- 创建和使用数据库资源计划

    (参考 http://blog.csdn.net/mrluoe/article/details/7969436 -- 整理并实践通过) 第1步,创建3个用户 SQL> create user s ...