(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 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末 ...
随机推荐
- wince开发环境搭建与全套教程
http://www.cnblogs.com/zhchongyao/archive/2010/12/28/1919176.html http://blog.csdn.net/weiren2006/ar ...
- 性能监控之Java程序执行解析
大家好,最近接触javassist技术,研究过程中对Java程序执行过程进行了一系列探索,弄清楚了几个盲区(仅针对个人而言),现将经验与大家分享. 1.编码->.java 通常指写代码的过程,最 ...
- HDU 3782 xxx定律
xxx定律 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- Class path contains multiple SLF4J bindings
[logback不同版本jar包] SLF4J: Class path contains multiple SLF4J bindings.SLF4J: Found binding in [jar:fi ...
- windows visual studio 2015安装
下载 首先到微软官方下载在线安装文件 https://www.microsoft.com 运行安装 点击运行后选择 自定义 选择安装功能,把sql server去掉,是数据库插件 点击下一步,中途会提 ...
- 一篇知乎的故事 - javascript技术贴
前言 就像文章题目所示,本文的发表源于知乎的一篇文章.文章链接如下:如果你想靠前端技术还房贷,你不能连这个都不会.这篇文章是群里水群时别人发的,像我这样的菜鸟角色才不会逛知乎~~~.这篇文章主要是讲了 ...
- 初学jQuery之jQuery事件与动画
今天我们就谈谈jquery中的事件和简单动画吧,它们毕竟基础是进阶华丽的根本!! 1.事件 1.window事件 ready 准备就绪 2.鼠标事件 方法 ...
- devexpress表格控件gridcontrol特殊应用(一)——实现禁用特定行(附源代码)
一些特殊的项目中会存在一些特殊需求,如需要禁用特定行.这时候gridcontrol的一般属性是实现不了的,就需要做一些更改.这时候你就需要去devexpress官网中找寻些资料(官网https://w ...
- linux下载时提示请尝试移除磁盘中不需要的文件并重试,或者保存到其他位置
因为我是用虚拟机装的linux,所以当时就分配了20G硬盘,下载了几个应用后再下载就提示我这个了.一开始我还以为是因为下载链接的问题,后来才知道原来是因为/tmp的满了. 然后我输入以下连个命令就能正 ...
- window编程之win程序框架
int APIENTRY _tWinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPTSTR lpCmd ...