序列流 SequenceInputStream
SequenceInputStream:序列流,对多个流进行合并。
SequenceInputStream 表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
序列流的使用:
可以将多个流串联到一起 ,然后就通过串联一个一来读取流中的数据。
--- SequenceInputStream 只能操作输入流。
方式二和方式三即为SequenceInputStream的用法:
package com.beiwo.io; import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import java.util.Vector; public class demo6 {
/**
*
*/
public static void main(String[] args) throws Exception {
// TODO 自动生成的方法存根
testMerge1();
testMerge2();
testMerge3();
}
// 方式三:可以同时操作多个文件夹
public static void testMerge3() throws Exception {
// 获取目标文件、
File file1 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\1.txt");
File file2 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\2.txt");
File file3 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\3.txt");
File file4 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\4.txt");
// 如果没有目标文件就创建
if (!file1.exists()) {
file1.createNewFile();
}
if (!file2.exists()) {
file2.createNewFile();
}
if (!file3.exists()) {
file3.createNewFile();
}
if (!file4.exists()) {
file4.createNewFile();
}
// 建立通道
FileInputStream fileInputStream1 = new FileInputStream(file1);
FileInputStream fileInputStream2 = new FileInputStream(file2);
FileInputStream fileInputStream3 = new FileInputStream(file3);
FileOutputStream fileOutputStream = new FileOutputStream(file4);
// 创建一个vector集合对象
Vector<FileInputStream> vector = new Vector<FileInputStream>();
// 添加
vector.add(fileInputStream1);
vector.add(fileInputStream2);
vector.add(fileInputStream3);
// 获取迭代器
Enumeration<FileInputStream> enumeration = vector.elements();
// 通过序列化将三个流串起来
SequenceInputStream sequenceInputStream = new SequenceInputStream(enumeration);
// 创建字节数组
byte[] b = new byte[1024];
// 读取数据
int length = 0;
while ((length = sequenceInputStream.read(b)) != -1) {
// 写入数据
fileOutputStream.write(b, 0, length);
}
// 关闭流
fileOutputStream.close();
sequenceInputStream.close();
} // 方式二:简化方式一的操作
public static void testMerge2() throws Exception {
// 获取目标文件、
File file1 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\1.txt");
File file2 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\2.txt");
File file3 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\3.txt");
// 如果没有目标文件就创建
if (!file1.exists()) {
file1.createNewFile();
}
if (!file2.exists()) {
file2.createNewFile();
}
if (!file3.exists()) {
file3.createNewFile();
}
// 建立通道
FileInputStream fileInputStream1 = new FileInputStream(file1);
FileInputStream fileInputStream2 = new FileInputStream(file2);
FileOutputStream fileOutputStream = new FileOutputStream(file3);
// 建立序列流
SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1, fileInputStream2);
byte[] b = new byte[1024];
// 读取数据
int length = 0;
while ((length = sequenceInputStream.read(b)) != -1) {
// 写入数据
fileOutputStream.write(b, 0, length);
}
// 关闭流
fileOutputStream.close();
sequenceInputStream.close();
} // 方法一:操作太复杂了
public static void testMerge1() throws Exception {
// 获取目标文件、
File file1 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\1.txt");
File file2 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\2.txt");
File file3 = new File("C:\\Users\\cdlx2016\\Desktop\\2\\3.txt");
// 如果没有目标文件就创建
if (!file1.exists()) {
file1.createNewFile();
}
if (!file2.exists()) {
file2.createNewFile();
}
if (!file3.exists()) {
file3.createNewFile();
}
// 建立通道
FileInputStream fileInputStream1 = new FileInputStream(file1);
FileInputStream fileInputStream2 = new FileInputStream(file2);
FileOutputStream fileOutputStream = new FileOutputStream(file3);
// 用集合来存输入流
List<FileInputStream> list = new ArrayList<FileInputStream>();
// 添加元素
list.add(fileInputStream1);
list.add(fileInputStream2);
// 边读边写数据
byte[] b = new byte[1024];
int length = 0;
for (int i = 0; i < list.size(); i++) {
while ((length = list.get(i).read(b)) != -1) {
// 写入数据
fileOutputStream.write(b, 0, length);
}
}
// 关闭流 先开后关
fileOutputStream.close();
fileInputStream2.close();
fileInputStream1.close();
} }
序列流 SequenceInputStream的更多相关文章
- IO流(五)__文件的递归、Properties、打印流PrintStream与PrintWriter、序列流SequenceInputStream
一.文件的遍历 1.需求:对指定目录进行所有的内容的列出(包含子目录的内容)-文件的深度遍历 思想:递归的思想,在递归的时候要记住递归的层次. public class FileTest { publ ...
- Java之序列流SequenceInputStream
序列流:作用就是将多个读取流合并成一个读取流,实现数据的合并 序列流表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到文件的末尾,接着从第二个输入流读取,以此类推:这样 ...
- JAVA学习第五十四课 — IO流(八)打印流 & 序列流
一.综合练习-文件清单列表 获取指定文件夹下,指定扩展名的文件(含子文件夹),并将这些文件的绝对路径写到一个文本文件里.也就是建立一个指定扩展名的文件列表 1.深度遍历 2.过滤器->容器 3. ...
- (20)IO流之SequenceInputStream 序列流
序列流,对多个流进行合并. SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直 ...
- IO流(SequenceInputStream序列流--文件拆分与合并)
一.文件拆分 1.将一个文件按照规定的大小进行分割成多个文件并将被分割详细信息保存至配置信息中 Properties 类表示了一个持久的属性集.Properties 可保存在流中或从流中加载,属性列表 ...
- Java:IO流其他类(字节数组流、字符数组流、数据流、打印流、Properities、对象流、管道流、随机访问、序列流、字符串读写流)
一.字节数组流: 类 ByteArrayInputStream:在构造函数的时候,需要接受数据源,而且数据源是一个字节数组. 包含一个内部缓冲区,该缓冲区包含从流中读取的字节.内部计数器跟踪 read ...
- java IO之 序列流 集合对象Properties 打印流 流对象
序列流 也称为合并流. SequenceInputStream 序列流,对多个流进行合并. SequenceInputStream 表示其他输入流的逻辑串联.它从输入流的有序集合开始,并从 第一个输入 ...
- Java基础---IO(二)--File类、Properties类、打印流、序列流(合并流)
第一讲 File类 一.概述 1.File类:文件和目录路径名的抽象表现形式 2.特点: 1)用来将文件或文件夹封装成对象 2)方便于对文件与文件夹的属性信息进行操作 3)File类的实例是不 ...
- IO流_SequenceInputStream(序列流)
SequenceInputStream(序列流):就是将多个流合成一个有序的流 需求:将三个文件中的数据合并到一个文件中 import java.io.FileInputStream; import ...
随机推荐
- 大熊君学习html5系列之------XHR2(XMLHttpRequest Level 2)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,(*^__^*) 嘻嘻……,这系列文章主要是学习Html5相关的知识点,以学习API知识点为入口,由浅入深的引入实例, 让大家一步一步的体会" ...
- 《大道至简》第一章读后感--JAVA语言伪代码形式
import.java.大道至简.*; //一·愚公移山 import.java.愚公移山.*; public class YuGongYiShan { //原始需求:惩山北之塞,出入之迂 //项目沟 ...
- FindWindowEx用法
函数原型:HWND FindWindowEx(HWND hwndParent,HWND hwndChildAfter,LPCTSTR lpszClass,LPCTSTR lpszWindow): 参数 ...
- PhpStorm 2016.3 For Mac 重大里程碑更新 -- 终于解决了不能输入中文标点符号的重大bug
PhpStorm 2016.3 For Mac 重大里程碑更新 1.[终于解决了]不能输入中文标点符号的重大bug,如 逗号“,”.“.”: 2.可以在一个窗体中,同时打开多个项目: 3.其他... ...
- ReactiveCocoa源码拆分解析(四)
(整个关于ReactiveCocoa的代码工程可以在https://github.com/qianhongqiang/QHQReactive下载) 上一章节简要的说明了如何实现的热信号.但是像那么写, ...
- python3 黑板客爬虫闯关游戏(二)
第二关猜登录密码,需要用到urllib.request和urllib.parse 也很简单,给代码 import urllib.request as ur import urllib.parse as ...
- ecshop后台增加|添加商店设置选项和使用方法详解
有时候我们想在Ecshop后台做个设置.radio.checkbox 等等来控制页面的显示,看看Ecshop的设计,用到了shop_config这个商店设置功能 Ecshop后台增加|添加商店设置选项 ...
- 十六天 css汇总、js汇总、dom汇总
1.css补充之 后台管理界面 顶部导航栏.左边菜单栏.右边内容栏固定在屏幕相应位置 会有上下左右滚动条,设定窗口最小值,使页面不乱.注意overflow:auto要与position:absol ...
- RobotFrameWork(四)变量运算与Evaluate
一.特殊变量运算: 执行结果: 二.Evaluate使用 函数释义:Evaluate是执行Python表达式,并返回执行结果 示例1: 执行结果: 示例2: 执行结果:
- CentOS 默认进入图形界面与文本界面
查看/etc/inittab文件,得到以下信息: # inittab is no longer used when using systemd.## ADDING CONFIGURATION HERE ...