(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 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末 ...
随机推荐
- Yii2 独立操作
看到最近有些人在问 yii2 独立操作相关的东西,在这做简单的说明吧, 平时核心业务逻辑一般用的还是比较少的.因为 独立操作 出现的原因 是 对重复被使用的操作进行简化,或 分配一个 额外处理逻辑的 ...
- Android开发知识体系总结
- 从jvm的角度来看单例模式
最近在看jvm,发现随着自己对jvm底层的了解,现在对java代码可以说是有了全新的认识.今天就从jvm的角度来看一看以前自以为很了解的单例模式. 了解单例模式的人都知道,单例模式有两种:" ...
- 《JAVASCRIPT高级程序设计》事件委托和模拟事件
由于事件处理程序可以为现代web应用提供交互能力,因此许多开发人员不分青红皂白向页面中添加大量的处理程序:这在某些语言中不会导致问题,但是在javascript,事件处理程序数量直接关系到页面的整体运 ...
- Import Statements 导入语句
Syntax of an Import Statement 导入语句的语法 An import statement allows clients to tell the engine which mo ...
- 网站SEO,HTTP请求的关键数字----6
客户端浏览器向服务器请求一个网页素材. 那么网页素材是通过什么方式,什么顺序被下载下来的呢. 我今天做了个简单的测试. 首先,准备测试文件 写一个网页,网页中引用若干的资源文件. 同一文件的不同的参数 ...
- oozie配置安装与原理
概述 当前开源的hadoop任务工作流管理主要有oozie和Azkaban,本文先介绍oozie的配置安装与基本运行原理. 配置安装 (参考https://segmentfault.com/a/11 ...
- SharePoint 2016 配置用户请求应用程序
最近看了看SharePoint的应用程序,觉得还是不错的,以前都没怎么注意过这样的功能.当然,应用程序除了让用户和管理员添加外,还可以让他们进行请求,把应用程序添加到应用程序目录,然后由统一的管理员进 ...
- sql decimal & float & celling 介绍
decimal 可以用在指定几个位数比如 123.456, decimal(3,3), 用这类型计算比较准确. 默认情况下,将数字转换为较低精度和小数位数的 decimal 或 numeric 值时, ...
- Hibernate一对多双向关联映射
建立多对一的单向关联关系 Emp.java private Integer empNo //员工编号 private String empName / ...