(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]Python2编码问题
以下内容说的都是 python 2.x 版本 简介 基本概念 Python "帮"你做的事情 推荐姿势 基本概念 我们看到的输入输出都是'字符'(characters),计算机(程 ...
- 关于Node.js后端架构的一点后知后觉
前言 上周有幸和淘宝前端团队的七念老师做了一些NodeJS方面上的交流(实际情况其实是他电话面试了我╮(╯-╰)╭),我们主要聊到了我参与维护的一个线上NodeJS服务,关于它的现状和当下的不足.他向 ...
- 基于机器学习的web异常检测
基于机器学习的web异常检测 Web防火墙是信息安全的第一道防线.随着网络技术的快速更新,新的黑客技术也层出不穷,为传统规则防火墙带来了挑战.传统web入侵检测技术通过维护规则集对入侵访问进行拦截.一 ...
- Webappbuilder开发快速预览
Webappbuilder开发快速预览 by 李远祥 Webappbuilder for ArcGIS 是由ArcGIS JavaScripit API和dojo创建的,它允许通过创建自己的widge ...
- bzoj1492--斜率优化DP+cdq分治
显然在某一天要么花完所有钱,要么不花钱. 所以首先想到O(n^2)DP: f[i]=max{f[i-1],(f[j]*r[j]*a[i]+f[j]*b[i])/(a[j]*r[j]+b[j])},j& ...
- Dynamics CRM 2015-Form之添加Ribbon Button
说到在CRM Form上添加Ribbon Button,那就不得不提到一个Tool:Ribbon Workbench,使用这个Tool,能为我们添加button带来不少便利. Ribbon Workb ...
- excel表格的特殊需求引发的Java思考
前言: 前些天遇到了这样的一个需求,将下图: 将表格中货号-前面部分一致的行合成一行,并且将第二行,第三行的价格添加到第一行中为价格二,价格三.如图: 接到这样的需求,我的第一感觉是直接手动合并(暗暗 ...
- winsshfs的快速入手
之前在公司使用mac ,并且通过mac下的osfuse和sshfs连接,直接将虚拟机的文件目录同步到了本地,并且可以进行实时操作修改,对于写项目,确实是省了很大一部分上传的精力. 于是在自己的win下 ...
- 一个Python小白5个小时爬虫经历
前言 最近业余在做一个基于.NET Core的搜索项目,奈何基层代码写好了,没有看起来很华丽的数据供测试.很巧的也是博客搜索,于是乎想到了博客园.C#也能做做页面数据抓取的,不过在博客园看到的大部分都 ...
- 用C++编一程序,先输出一行sun mon tue wed thu fri fri,接着使用右对齐打印出日期,像日历那样
用C++编一程序,先输出一行sun mon tue wed thu fri fri,接着使用右对齐打印出日期,像日历那样 先输出一行sun mon tue wed thu fri fri,再提醒用户输 ...