序列流 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 ...
随机推荐
- jquery numberbox赋值
numberbox不能使用$('#id').val( '');只能使用$('#id').numberbox('setValue','');
- UIScrollView的基本使用
UIScrollView的用法很简单 将需要展示的内容添加到UIScrollView中 设置UIScrollView的contentSize属性,告诉UIScrollView所有内容的尺寸,也就是告诉 ...
- getcwd()和dirname(__FILE__)的区别
我个人理解:getcwd()会随着包含文件的改变而改变,而dirname(__FILE__)不会.即 getcwd() 表示获取当前执行文件的物理路径. 如 getcwd()显示: /www/proj ...
- C/C++内存、指针问题
转 http://wenku.baidu.com/link?url=tN9Fac-XyB2F7V7xwYcRclu464G2c8ybYMBxNXbBGQJXEEy0vJxTOzcAeVrFrqYLfj ...
- poj 1733
这题离散化+并查集,没看出关dp什么事.(那他为什么放到dp里面) 用Si记录前i项的和.拆成两个点,i*2表示与第i个相同,i*2+1表示与第i个不同.用并查集判断.区间[a,b]就可以看成Sb-S ...
- jQuery UI Datepicker
http://www.runoob.com/try/try.php?filename=jqueryui-example-datepicker-dropdown-month-year <!doct ...
- this a a mark down test
*** this is title ''' code '''
- ASP.NET知识总结(3.HTTP协议详解)
引言 HTTP是一个属于应用层的面向对象的协议,由于其简捷.快速的方式,适用于分布式超媒体信息系统.它于1990年提出,经过几年的使用与发展,得到不断地完善和扩展.目前在WWW中使用的是HTTP/1. ...
- 【leetcode】Perfect Squares (#279)
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 1 ...
- Oracle资源管理器(二)-- 创建和使用数据库资源计划
(参考 http://blog.csdn.net/mrluoe/article/details/7969436 -- 整理并实践通过) 第1步,创建3个用户 SQL> create user s ...