(20)IO流之SequenceInputStream 序列流
序列流,对多个流进行合并。
SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
序列流,对多个流进行合并。
SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
合并两个流
使用构造函数SequenceInputStream(InputStream s1, InputStream s2)
//使用SequenceInputStream合并两个文件
public static void merge2() throws IOException{
//找到目标文件
File inFile1 = new File("d:\\a.txt");
File inFile2 = new File("d:\\b.txt");
File outFile = new File("D:\\c.txt");
//建立数据通道
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
FileInputStream fileInputStream1 = new FileInputStream(inFile1);
FileInputStream fileInputStream2 = new FileInputStream(inFile2); //建立序列流对象
SequenceInputStream inputStream = new SequenceInputStream(fileInputStream1,fileInputStream2);
byte[] buf = new byte[];
int length = ;
while((length = inputStream.read(buf))!=-)
{
fileOutputStream.write(buf, , length);
}
inputStream.close();
// fileInputStream1.close(); //查看上面inputStream.close()的源码就可以看到,它穿起来的流一块关闭了
// fileInputStream2.close();
fileOutputStream.close();
}
合并多个文件
//把三个文件合并成一个文件
public static void merge3() throws IOException
{
//找到目标文件
File file1 = new File("d:\\a.txt");
File file2 = new File("d:\\b.txt");
File file3 = new File("D:\\c.txt");
File file4 = new File("d:\\d.txt");
//建立对应的输入输出流对象
FileInputStream fileInputStream1 = new FileInputStream(file1);
FileInputStream fileInputStream2 = new FileInputStream(file2);
FileInputStream fileInputStream3 = new FileInputStream(file3);
FileOutputStream fileOutputStream = new FileOutputStream(file4); //创建序列流对象
Vector<FileInputStream> vector = new Vector<FileInputStream>();
vector.add(fileInputStream1);
vector.add(fileInputStream2);
vector.add(fileInputStream3);
Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream inputStream = new SequenceInputStream(e); //读取文件的数据
int length =;
byte[] buf = new byte[];
while((length = inputStream.read(buf))!=-)
{
fileOutputStream.write(buf, , length);
}
inputStream.close();
fileOutputStream.close();
}
下面是一个例子把一个mp3文件切割合并的过程:
public class Demo2
{
public static void main(String[] args) throws IOException
{
cutFile();
mergeFile();
} //合并
public static void mergeFile() throws IOException
{
//找到目标文件
File dir = new File("D:\\part");
Vector<FileInputStream> vector = new Vector<FileInputStream>();
//通过目标文件夹找到所有的mmp3并添加到Vector中
File[] files = dir.listFiles();
for (File file : files)
{
if(file.getName().endsWith("mp3"))
{
vector.add(new FileInputStream(file));
}
} //通过vector获取迭代器对象
Enumeration<FileInputStream> e = vector.elements(); SequenceInputStream inputStream = new SequenceInputStream(e); //建立文件的输出通道
FileOutputStream fileOutputStream = new FileOutputStream("D:\\merge.mp3");
//建立缓冲数组
int length = ;
byte[] buf = new byte[*];
while((length = inputStream.read(buf))!=-)
{
fileOutputStream.write(buf, , length);
}
} //切割
public static void cutFile() throws IOException
{
File file = new File("D:\\1.mp3");
//目标文件夹
File dir = new File("D:\\part");
//建立数据的输入通道
FileInputStream fileInputStream = new FileInputStream(file);
//建立缓存数组存储
byte[] buf = new byte[*];
int length = ;
for(int i = ; (length = fileInputStream.read(buf))!=-; i ++)
{
FileOutputStream fileOutputStream = new FileOutputStream(new File(dir, "part"+i+".mp3"));
fileOutputStream.write(buf, , length);
fileOutputStream.close();
}
//关闭资源
fileInputStream.close();
}
}
(20)IO流之SequenceInputStream 序列流的更多相关文章
- IO流(SequenceInputStream序列流--文件拆分与合并)
一.文件拆分 1.将一个文件按照规定的大小进行分割成多个文件并将被分割详细信息保存至配置信息中 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载,属性列表 ...
- java基础48 IO流技术(序列流)
本文知识点目录: 1.SequenceInputStream序列流的步骤 2.实例 3.附录(音乐的切割与合并) 1.SequenceInputStream序列流的步骤 1.找到目标文件 ...
- IO(三)----序列流
SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的 ...
- IO流_SequenceInputStream(序列流)
SequenceInputStream(序列流):就是将多个流合成一个有序的流 需求:将三个文件中的数据合并到一个文件中 import java.io.FileInputStream; import ...
- 序列流、对象操作流、打印流、标准输入输出流、随机访问流、数据输入输出流、Properties(二十二)
1.序列流 * 1.什么是序列流 * 序列流可以把多个字节输入流整合成一个, 从序列流中读取数据时, 将从被整合的第一个流开始读, 读完一个之后继续读第二个, 以此类推.* 2.使用方式 * 整合两个 ...
- IO流(五)__文件的递归、Properties、打印流PrintStream与PrintWriter、序列流SequenceInputStream
一.文件的遍历 1.需求:对指定目录进行所有的内容的列出(包含子目录的内容)-文件的深度遍历 思想:递归的思想,在递归的时候要记住递归的层次. public class FileTest { publ ...
- Java之序列流SequenceInputStream
序列流:作用就是将多个读取流合并成一个读取流,实现数据的合并 序列流表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到文件的末尾,接着从第二个输入流读取,以此类推:这样 ...
- 20.IO流部分笔记
20.IO流部分笔记 2018/09/06 1.IO流 1.1 创建字节输出流对象,如果没有就自动创建一个 FileOutputStram fos = new FileOutputStram(&qu ...
- 序列流 SequenceInputStream
SequenceInputStream:序列流,对多个流进行合并. SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末 ...
随机推荐
- 字符集编码与Python(二)Unicode与utf-8
Python中的Unicode和utf-8 上一篇提过了字符集的历史其中简单的讲解了Unicode与utf-8的关系,简单的总结一下: utf-8和utf-16 .utf-32是一类,实现的功能是 ...
- [html5] 学习笔记-html5增强的页面元素
在 HTML5 中,不仅增加了很多表单中的元素,同时也增加和改良了可以应用在整个页面中的元素.重点包含 figure.figcaption.details.summary.mark.progress. ...
- shift、unshift、 push、pop用法--JavaScript参考手册
转自http://www.blogbus.com/kingslay-logs/216353709.html shift()定义和用法shift() 方法用于把数组的第一个元素从其中删除,并返回第一个元 ...
- C++编程练习(3)----“实现简单的栈的顺序存储结构“
栈(stack)是限定仅在表尾进行插入和删除操作的线性表. 允许插入和删除的一端称为栈顶(top),另一端称为栈底(bottom). 栈又称为后进先出(Last In First Out)的线性表,简 ...
- Unsupported major.minor version 52.0错误解决 Ubuntu JDK8 安装配置
Unsupported major.minor version 52.0错误一般是因为应用程序需要JDK8而ubuntu默认的是jdk7,所以需要切换到jdk8才能解决这个问题. 本文使用PPA方式安 ...
- Java中正则表达式去除html标签
Java中正则表达式去除html的标签,主要目的更精确的显示内容,比如前一段时间在做类似于博客中发布文章功能,当编辑器中输入内容后会将样式标签也传入后台并且保存数据库,但是在显示摘要的时候,比如显示正 ...
- 从C#到TypeScript - 接口
总目录 从C#到TypeScript - 类型 从C#到TypeScript - 高级类型 从C#到TypeScript - 变量 从C#到TypeScript - 接口 从C#到TypeScript ...
- java-6数组
一. 请编写一个程序将一个整数转换为汉字读法字符串.比如"1123"转换为"一千一百二十三".更进一步,能否将数字表示的金额改为"汉字表达?比如将&q ...
- oozie配置安装与原理
概述 当前开源的hadoop任务工作流管理主要有oozie和Azkaban,本文先介绍oozie的配置安装与基本运行原理. 配置安装 (参考https://segmentfault.com/a/11 ...
- ZooKeeper的不同类型的Znodes
在ZooKeeper中有3个不同类型的znodes:persistent, ephemeral, sequential Persistent Znodes(默认)在ZooKeeper中,这是默认的zn ...