序列流:
SequenceInputStream 表示其他输入流的逻辑串联。对多个流进行合并。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文          件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
Demo1:利用序列流合并文件
  
  1. package com.cn.sequenceInputStream;
  2. import java.io.File;
  3. import java.io.FileInputStream;
  4. import java.io.FileNotFoundException;
  5. import java.io.FileOutputStream;
  6. import java.io.IOException;
  7. import java.io.InputStream;
  8. import java.io.SequenceInputStream;
  9. import java.util.ArrayList;
  10. import java.util.Enumeration;
  11. import java.util.Vector;
  12. /**
  13. * Author:Liu Zhiyong(QQ:1012421396)
  14. * Version:Version_1
  15. * Date:2016年7月30日20:59:06
  16. * Desc:
  17. 字节流:
  18. 输入字节流:
  19. ----------------| InputStream 所有输入字节流的基类, 抽象类
  20. ---------------------| FileInputStream 读取文件数据的输入字节流
  21. ---------------------| BufferedInputStream 缓冲输入字符流 ,该类出现的目的,是为了提高读取文件的效率,这个类其实只不过是在内部维护一个8Kb的字节数组而已。
  22. 输出字节流:
  23. ----------------| OutputStream 所有输出字节流的基类,抽象类
  24. ---------------------| FileOutputStream 向文件输出数据的输出字节流
  25. ---------------------| BufferedOutputStream 缓冲输出字节流,该类出现的目的,是为了提高向文件写数据的效率。这个类也只不过是在内部维护了一个8Kb的字节数组而已。
  26. 字符流: 字符流 = 字节流 + 编码(解码)
  27. 输入字符流:
  28. ----------------| Reader 所有输入字符流的基类,抽象类
  29. ---------------------| FileReader 读取文件数据的输入字符流
  30. ---------------------| BufferedReader 缓冲输入字符流。该类出现的目的是为了提高读取文件数据的效率与拓展FileReader的功能(readLine())。这个类的内部也只不过是在内部维护了一个8Kb的字符数组而已。
  31. ----------------| Writer 所有输出字符流的基类,抽象类
  32. ---------------------| FileWriter 向文件输出数据的输出字符流
  33. ---------------------| BufferedWriter 缓冲输出字符流,该类出现的目的,是为了提高向文件写数据的效率与拓展FileWriter的功能(newLine())。
  34. 序列流:
  35. SequenceInputStream 表示其他输入流的逻辑串联。对多个流进行合并。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
  36. */
  37. public class Demo1 {
  38. public static void main(String[] args) throws IOException {
  39. // merge1();
  40. // merge2();
  41. merge3();
  42. }
  43. public static void merge1() throws IOException{
  44. //找到目标文件
  45. File inFile1 = new File("f:/a.txt");
  46. File inFile2 = new File("f:/b.txt");
  47. File outFile = new File("f:/c.txt");
  48. //建立数据通道
  49. FileInputStream fileInputStream1 = new FileInputStream(inFile1);
  50. FileInputStream fileInputStream2 = new FileInputStream(inFile2);
  51. FileOutputStream fileOutputStream = new FileOutputStream(outFile);
  52. //把输入流存储到集合中再从集合中读取出来
  53. ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
  54. list.add(fileInputStream1);
  55. list.add(fileInputStream2);
  56. for(FileInputStream fileInputStream : list){
  57. //准备一个缓冲数组
  58. byte[] buf = new byte[1024];
  59. int length = 0;
  60. while((length = fileInputStream.read(buf)) != -1){
  61. fileOutputStream.write(buf, 0, length);
  62. }
  63. //关闭资源
  64. fileInputStream.close();
  65. }
  66. //关闭资源
  67. fileOutputStream.close();
  68. }
  69. /**
  70. * SequenceInputStream(InputStream s1, InputStream s2)
  71. 通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),
  72. 以提供从此 SequenceInputStream 读取的字节。
  73. * @throws IOException
  74. */
  75. public static void merge2() throws IOException{
  76. //找到目标文件
  77. File inFile1 = new File("f:/a.txt");
  78. File inFile2 = new File("f:/b.txt");
  79. File outFile = new File("f:/c.txt");
  80. //建立数据通道
  81. FileInputStream fileInputStream1 = new FileInputStream(inFile1);
  82. FileInputStream fileInputStream2 = new FileInputStream(inFile2);
  83. FileOutputStream fileOutputStream = new FileOutputStream(outFile);
  84. SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1, fileInputStream2);
  85. int length = 0;
  86. byte[] buf = new byte[1024];
  87. while((length = sequenceInputStream.read(buf)) != -1){
  88. fileOutputStream.write(buf, 0, length);
  89. }
  90. //关闭资源
  91. sequenceInputStream.close();
  92. fileOutputStream.close();
  93. }
  94. /** SequenceInputStream(Enumeration<? extends InputStream> e)
  95. 通过记住参数来初始化新创建的 SequenceInputStream,
  96. 该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。*/
  97. public static void merge3() throws IOException{
  98. //找到目标文件
  99. File inFile1 = new File("f:/a.txt");
  100. File inFile2 = new File("f:/b.txt");
  101. File inFile3 = new File("f:/c.txt");
  102. File outFile = new File("f:/d.txt");
  103. //建立数据通道
  104. FileInputStream fileInputStream1 = new FileInputStream(inFile1);
  105. FileInputStream fileInputStream2 = new FileInputStream(inFile2);
  106. FileInputStream fileInputStream3 = new FileInputStream(inFile3);
  107. FileOutputStream fileOutputStream = new FileOutputStream(outFile);
  108. Vector<InputStream> vector = new Vector<InputStream>();
  109. vector.add(fileInputStream1);
  110. vector.add(fileInputStream2);
  111. vector.add(fileInputStream3);
  112. Enumeration<InputStream> elements = vector.elements();
  113. SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
  114. int length = 0;
  115. byte[] buf = new byte[1024];
  116. while((length = sequenceInputStream.read(buf)) != -1){
  117. fileOutputStream.write(buf, 0, length);
  118. }
  119. //关闭资源
  120. sequenceInputStream.close();
  121. fileOutputStream.close();
  122. }
  123. }
  Demo2:把一首MP3文件切割成n份,然后再利用序列流合并
  
  1. package com.cn.sequenceInputStream;
  2. import java.awt.List;
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileOutputStream;
  7. import java.io.IOException;
  8. import java.io.SequenceInputStream;
  9. import java.util.ArrayList;
  10. import java.util.Enumeration;
  11. import java.util.Vector;
  12. /**
  13. * Author:Liu Zhiyong(QQ:1012421396)
  14. * Version:Version_1
  15. * Date:2016年7月31日09:35:57
  16. * Desc:
  17. 需求:把一首4.371M的mp3先切成5份,然后再把这些文件合并起来。
  18. */
  19. public class Demo2 {
  20. public static void main(String[] args) throws IOException {
  21. //先切割文件
  22. // cutFile1();
  23. cutFile2();
  24. //合并数据
  25. // mergeFile1();
  26. mergeFile2();
  27. }
  28. /**
  29. * 切割文件 方式1
  30. * @throws IOException
  31. */
  32. public static void cutFile1() throws IOException{
  33. //找到目标文件
  34. File srcFile = new File("f:/饿狼传说.mp3");
  35. //方式1
  36. File destFile1 = new File("f:/饿狼传说1.mp3");
  37. File destFile2 = new File("f:/饿狼传说2.mp3");
  38. File destFile3 = new File("f:/饿狼传说3.mp3");
  39. File destFile4 = new File("f:/饿狼传说4.mp3");
  40. File destFile5 = new File("f:/饿狼传说5.mp3");
  41. //建立数据通道
  42. FileInputStream fileInputStream = new FileInputStream(srcFile);
  43. FileOutputStream fileOutputStream1 = new FileOutputStream(destFile1);
  44. FileOutputStream fileOutputStream2 = new FileOutputStream(destFile2);
  45. FileOutputStream fileOutputStream3 = new FileOutputStream(destFile3);
  46. FileOutputStream fileOutputStream4 = new FileOutputStream(destFile4);
  47. FileOutputStream fileOutputStream5 = new FileOutputStream(destFile5);
  48. ArrayList<FileOutputStream> list = new ArrayList<FileOutputStream>();
  49. list.add(fileOutputStream1);
  50. list.add(fileOutputStream2);
  51. list.add(fileOutputStream3);
  52. list.add(fileOutputStream4);
  53. list.add(fileOutputStream5);
  54. //建立1M大小缓冲数组
  55. byte[] buf = new byte[1024*1024];
  56. int length = 0;
  57. int i = 0;
  58. while((length = fileInputStream.read(buf)) != -1){
  59. list.get(i).write(buf, 0, length);
  60. i++;
  61. }
  62. fileOutputStream4.close();
  63. fileOutputStream3.close();
  64. fileOutputStream2.close();
  65. fileOutputStream1.close();
  66. fileInputStream.close();
  67. }
  68. /**
  69. * 切割文件 方式2
  70. * @throws IOException
  71. */
  72. public static void cutFile2() throws IOException{
  73. //找到目标文件
  74. File srcFile = new File("f:/饿狼传说.mp3");
  75. File dir = new File("f:/cut_merge_music");
  76. //建立数据通道
  77. FileInputStream fileInputStream = new FileInputStream(srcFile);
  78. //建立1M大小缓冲数组
  79. byte[] buf = new byte[1024*1024];
  80. int length = 0;
  81. for(int i=1; (length = fileInputStream.read(buf)) != -1; i++ ){
  82. FileOutputStream fileOutputStream = new FileOutputStream(new File(dir, "part"+i+".mp3"));
  83. fileOutputStream.write(buf, 0, length);
  84. fileOutputStream.close(); //关闭资源
  85. }
  86. fileInputStream.close();
  87. }
  88. public static void mergeFile1() throws IOException{
  89. //找到目标文件
  90. File srcFile1 = new File("f:/饿狼传说1.mp3");
  91. File srcFile2 = new File("f:/饿狼传说2.mp3");
  92. File srcFile3 = new File("f:/饿狼传说3.mp3");
  93. File srcFile4 = new File("f:/饿狼传说4.mp3");
  94. File srcFile5 = new File("f:/饿狼传说5.mp3");
  95. File destFile = new File("f:/饿狼传说全.mp3");
  96. //建立数据通道
  97. FileInputStream fileInputStream1 = new FileInputStream(srcFile1);
  98. FileInputStream fileInputStream2 = new FileInputStream(srcFile2);
  99. FileInputStream fileInputStream3 = new FileInputStream(srcFile3);
  100. FileInputStream fileInputStream4 = new FileInputStream(srcFile4);
  101. FileInputStream fileInputStream5 = new FileInputStream(srcFile5);
  102. FileOutputStream fileOutputStream = new FileOutputStream(destFile);
  103. Vector<FileInputStream> list = new Vector<FileInputStream>();
  104. list.add(fileInputStream1);
  105. list.add(fileInputStream2);
  106. list.add(fileInputStream3);
  107. list.add(fileInputStream4);
  108. list.add(fileInputStream5);
  109. Enumeration<FileInputStream> elements = list.elements();
  110. //建立序列流
  111. SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
  112. //建立小缓冲数组
  113. byte[] buf = new byte[1024];
  114. int length = 0;
  115. int i = 0;
  116. //写出数据
  117. while((length = sequenceInputStream.read(buf)) != -1){
  118. fileOutputStream.write(buf, 0, length);
  119. }
  120. //关闭资源
  121. sequenceInputStream.close();
  122. fileOutputStream.close();
  123. }
  124. public static void mergeFile2() throws IOException{
  125. //找到目标文件
  126. File dir = new File("f:/cut_merge_music");
  127. File destFile = new File("f:/cut_merge_music/饿狼传说全.mp3");
  128. //通过目标文件夹找到所有的MP3文件,然后把所有的MP3文件添加到vector中
  129. Vector<FileInputStream> list = new Vector<FileInputStream>();
  130. File[] listFiles = dir.listFiles();
  131. for(File file : listFiles){
  132. if(file.getName().endsWith(".mp3")){
  133. list.add(new FileInputStream(file));
  134. }
  135. }
  136. //通过Vector获取迭代器
  137. Enumeration<FileInputStream> elements = list.elements();
  138. //建立文件 的输出通道
  139. FileOutputStream fileOutputStream = new FileOutputStream(destFile);
  140. //建立序列流
  141. SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
  142. //建立小缓冲数组
  143. byte[] buf = new byte[1024];
  144. int length = 0;
  145. int i = 0;
  146. //写出数据
  147. while((length = sequenceInputStream.read(buf)) != -1){
  148. fileOutputStream.write(buf, 0, length);
  149. }
  150. //关闭资源
  151. sequenceInputStream.close();
  152. fileOutputStream.close();
  153. }
  154. }

序列流_SequenceInputStream类相关推荐

  1. Java基础IO流概述、字符流、字节流、流操作规律、File类、Properties类、打印流、序列流

    IO流:(Input Output)流 字符流的由来:其实就是字节流读取文字字节数据后,不直接操作而是先查指定的码表,获取对应的文字进行操作 简单说:字符流 = 字节流 + 编码表 字节流的两个顶层父 ...

  2. 5.2 IO流(File类,Propertis配置文件,其他类(打印流,序列流,操作对象的流(序列化接口),随机访问文件的流,管道流,操作基本数据的流,操作数组的流,操作字符串的流),编码表)

    1.File类 IO流的流对象只能操作设备上的数据.File类:1.用来将文件或者文件夹(也称目录)封装成对象. 2.方便对文件和文件夹的属性信息进行操作.(操作文件夹,文件的属性(创建时间,修改时间 ...

  3. JAVA File类、IO流体验与简介(字节流、字符流、序列流、打印流、编码、递归)

    1. File类 1.1. File类说明 存储在变量,数组和对象中的数据是暂时的,当程序终止时他们就会丢失.为了能够永 久的保存程序中创建的数据,需要将他们存储到硬盘或光盘的文件中.这些文件可以移动 ...

  4. 第三次学JAVA再学不好就吃翔(part111)--序列流

    学习笔记,仅供参考,有错必纠 序列流 序列流可以把多个字节输入流整合成一个,从序列流中读取数据时,将从被整合的第一个流开始读,读完一个之后继续读第二个,以此类推. SequenceInputStrea ...

  5. day22 Java学习 IO流(序列流)

    IO流(序列流) 序列流: * 可以把多个字节输入流整合成一个,从序列流中读取数据时,将从被整合的第一个流开始读,读完一个之后继续读第二个. 整合方式: * Seq uenceInputStream ...

  6. 【Java】day21--装饰者设计模式、序列流对象、对象输入输出流、Properties配置文件部分知识点总结

    (一)继承增强一个类的功能 BufferedReader    对FileReader拓展了一个功能,readLine. 需求1:编写一个类对BufferedReader的功能进行增强,增强其read ...

  7. 第十三章、IO流_File类与递归/基本流/增强流/属性集与打印流__黑马Java第57期个人学习笔记_个人笔记

    第一节.File类与递归 一.File类 (一)概念: 1.File类是文件和目录路径名的抽象表示,主要用于文件和目录的创建.查找和删除等操作.(也重写了toString为getPath) 2.绝对路 ...

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

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

  9. JAVASE基础模块三十三(Scanner 扫描流 RandomAccessFile随机访问流 ObjectInputStream序列流 Properties集合流 Sequence顺序流

    JAVASE基础模块三十三(Scanner 扫描流 RandomAccessFile随机访问流 ObjectInputStream序列流 Properties集合流 SequenceInputStre ...

最新文章

  1. Teamviewer 远程ssh命令行更改密码启动
  2. Python Module_Socket_网络编程
  3. CSS学习02之css导入方式
  4. JAVA CXF、XFIRE、AXIS webservice获取客户端IP
  5. mongodb 日期分组聚合_mongoose聚合aggregate按日期分组计算
  6. IDEA Tomcat Catalina Log出现乱码
  7. poj3667 区间合并,找最左边的空余块
  8. python list 的深浅拷贝探索
  9. react-native 无网络提示_win7系统无线网络提示ipv4无访问权限怎么解决【解决方法】...
  10. lxml简单用法 解析网页
  11. SQL Server2012安装教程
  12. 多窗口键盘鼠标同步软件
  13. OSChina 周三乱弹 —— 你们的女神宣布结婚了
  14. 解决win10安装失败原因和方法
  15. 为什么主机IP地址通常以192.168开头?
  16. (Java实习生)每日10道面试题打卡——Java基础知识篇2
  17. 扬子苦荞啤酒 一杯苦荞啤酒,精彩你的世界
  18. 租用游艇问题 石子合并问题 动态规划实验
  19. 计算机d盘变为raw,d盘文件系统raw 硬盘变成raw格式怎么办
  20. 回文数函数的粗浅理解

热门文章

  1. 英飞凌TC3xx_MCMCAN(一)
  2. 文件加密:闪灵文件夹锁免费版 1.1——锁住自己的小秘密
  3. 前端学习day28:响应式布局、阿里图标等
  4. 基于SSM+Layui的图书管理系统(附源码+数据库)
  5. Java程序员必备安装软件清单
  6. 十天学会php 零基础,十天学会PHP之第九天
  7. Java中遍历集合的并发修改异常解决方案
  8. 三角形面积公式用坐标用c语言,通过三个点的坐标求出三角形面积的公式
  9. Java程序员水平有几个层次?需要掌握哪些技术?
  10. 方法递归调用(重点)