序列流_SequenceInputStream类
来源:互联网 发布:网络捕鱼游戏加盟代理 编辑:程序博客网 时间:2024/05/17 02:44
序列流:
SequenceInputStream 表示其他输入流的逻辑串联。对多个流进行合并。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文 件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
Demo1:利用序列流合并文件
package com.cn.sequenceInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2016年7月30日20:59:06
* Desc:
字节流:
输入字节流:
----------------| InputStream 所有输入字节流的基类, 抽象类
---------------------| FileInputStream 读取文件数据的输入字节流
---------------------| BufferedInputStream 缓冲输入字符流 ,该类出现的目的,是为了提高读取文件的效率,这个类其实只不过是在内部维护一个8Kb的字节数组而已。
输出字节流:
----------------| OutputStream 所有输出字节流的基类,抽象类
---------------------| FileOutputStream 向文件输出数据的输出字节流
---------------------| BufferedOutputStream 缓冲输出字节流,该类出现的目的,是为了提高向文件写数据的效率。这个类也只不过是在内部维护了一个8Kb的字节数组而已。
字符流: 字符流 = 字节流 + 编码(解码)
输入字符流:
----------------| Reader 所有输入字符流的基类,抽象类
---------------------| FileReader 读取文件数据的输入字符流
---------------------| BufferedReader 缓冲输入字符流。该类出现的目的是为了提高读取文件数据的效率与拓展FileReader的功能(readLine())。这个类的内部也只不过是在内部维护了一个8Kb的字符数组而已。
----------------| Writer 所有输出字符流的基类,抽象类
---------------------| FileWriter 向文件输出数据的输出字符流
---------------------| BufferedWriter 缓冲输出字符流,该类出现的目的,是为了提高向文件写数据的效率与拓展FileWriter的功能(newLine())。
序列流:
SequenceInputStream 表示其他输入流的逻辑串联。对多个流进行合并。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
*/
public class Demo1 {
public static void main(String[] args) throws IOException {
//merge1();
//merge2();
merge3();
}
public static void merge1() throws IOException{
//找到目标文件
File inFile1 = new File("f:/a.txt");
File inFile2 = new File("f:/b.txt");
File outFile = new File("f:/c.txt");
//建立数据通道
FileInputStream fileInputStream1 = new FileInputStream(inFile1);
FileInputStream fileInputStream2 = new FileInputStream(inFile2);
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
//把输入流存储到集合中再从集合中读取出来
ArrayList<FileInputStream> list = new ArrayList<FileInputStream>();
list.add(fileInputStream1);
list.add(fileInputStream2);
for(FileInputStream fileInputStream : list){
//准备一个缓冲数组
byte[] buf = new byte[1024];
int length = 0;
while((length = fileInputStream.read(buf)) != -1){
fileOutputStream.write(buf, 0, length);
}
//关闭资源
fileInputStream.close();
}
//关闭资源
fileOutputStream.close();
}
/**
* SequenceInputStream(InputStream s1, InputStream s2)
通过记住这两个参数来初始化新创建的 SequenceInputStream(将按顺序读取这两个参数,先读取 s1,然后读取 s2),
以提供从此 SequenceInputStream 读取的字节。
* @throws IOException
*/
public static void merge2() throws IOException{
//找到目标文件
File inFile1 = new File("f:/a.txt");
File inFile2 = new File("f:/b.txt");
File outFile = new File("f:/c.txt");
//建立数据通道
FileInputStream fileInputStream1 = new FileInputStream(inFile1);
FileInputStream fileInputStream2 = new FileInputStream(inFile2);
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
SequenceInputStream sequenceInputStream = new SequenceInputStream(fileInputStream1, fileInputStream2);
int length = 0;
byte[] buf = new byte[1024];
while((length = sequenceInputStream.read(buf)) != -1){
fileOutputStream.write(buf, 0, length);
}
//关闭资源
sequenceInputStream.close();
fileOutputStream.close();
}
/**SequenceInputStream(Enumeration<? extends InputStream> e)
通过记住参数来初始化新创建的 SequenceInputStream,
该参数必须是生成运行时类型为 InputStream 对象的 Enumeration 型参数。*/
public static void merge3() throws IOException{
//找到目标文件
File inFile1 = new File("f:/a.txt");
File inFile2 = new File("f:/b.txt");
File inFile3 = new File("f:/c.txt");
File outFile = new File("f:/d.txt");
//建立数据通道
FileInputStream fileInputStream1 = new FileInputStream(inFile1);
FileInputStream fileInputStream2 = new FileInputStream(inFile2);
FileInputStream fileInputStream3 = new FileInputStream(inFile3);
FileOutputStream fileOutputStream = new FileOutputStream(outFile);
Vector<InputStream> vector = new Vector<InputStream>();
vector.add(fileInputStream1);
vector.add(fileInputStream2);
vector.add(fileInputStream3);
Enumeration<InputStream> elements = vector.elements();
SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
int length = 0;
byte[] buf = new byte[1024];
while((length = sequenceInputStream.read(buf)) != -1){
fileOutputStream.write(buf, 0, length);
}
//关闭资源
sequenceInputStream.close();
fileOutputStream.close();
}
}
Demo2:把一首MP3文件切割成n份,然后再利用序列流合并
package com.cn.sequenceInputStream;
import java.awt.List;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Vector;
/**
* Author:Liu Zhiyong(QQ:1012421396)
* Version:Version_1
* Date:2016年7月31日09:35:57
* Desc:
需求:把一首4.371M的mp3先切成5份,然后再把这些文件合并起来。
*/
public class Demo2 {
public static void main(String[] args) throws IOException {
//先切割文件
//cutFile1();
cutFile2();
//合并数据
//mergeFile1();
mergeFile2();
}
/**
* 切割文件 方式1
* @throws IOException
*/
public static void cutFile1() throws IOException{
//找到目标文件
File srcFile = new File("f:/饿狼传说.mp3");
//方式1
File destFile1 = new File("f:/饿狼传说1.mp3");
File destFile2 = new File("f:/饿狼传说2.mp3");
File destFile3 = new File("f:/饿狼传说3.mp3");
File destFile4 = new File("f:/饿狼传说4.mp3");
File destFile5 = new File("f:/饿狼传说5.mp3");
//建立数据通道
FileInputStream fileInputStream = new FileInputStream(srcFile);
FileOutputStream fileOutputStream1 = new FileOutputStream(destFile1);
FileOutputStream fileOutputStream2 = new FileOutputStream(destFile2);
FileOutputStream fileOutputStream3 = new FileOutputStream(destFile3);
FileOutputStream fileOutputStream4 = new FileOutputStream(destFile4);
FileOutputStream fileOutputStream5 = new FileOutputStream(destFile5);
ArrayList<FileOutputStream> list = new ArrayList<FileOutputStream>();
list.add(fileOutputStream1);
list.add(fileOutputStream2);
list.add(fileOutputStream3);
list.add(fileOutputStream4);
list.add(fileOutputStream5);
//建立1M大小缓冲数组
byte[] buf = new byte[1024*1024];
int length = 0;
int i = 0;
while((length = fileInputStream.read(buf)) != -1){
list.get(i).write(buf, 0, length);
i++;
}
fileOutputStream4.close();
fileOutputStream3.close();
fileOutputStream2.close();
fileOutputStream1.close();
fileInputStream.close();
}
/**
* 切割文件 方式2
* @throws IOException
*/
public static void cutFile2() throws IOException{
//找到目标文件
File srcFile = new File("f:/饿狼传说.mp3");
File dir = new File("f:/cut_merge_music");
//建立数据通道
FileInputStream fileInputStream = new FileInputStream(srcFile);
//建立1M大小缓冲数组
byte[] buf = new byte[1024*1024];
int length = 0;
for(int i=1; (length = fileInputStream.read(buf)) != -1; i++ ){
FileOutputStream fileOutputStream = new FileOutputStream(new File(dir, "part"+i+".mp3"));
fileOutputStream.write(buf, 0, length);
fileOutputStream.close(); //关闭资源
}
fileInputStream.close();
}
public static void mergeFile1() throws IOException{
//找到目标文件
File srcFile1 = new File("f:/饿狼传说1.mp3");
File srcFile2 = new File("f:/饿狼传说2.mp3");
File srcFile3 = new File("f:/饿狼传说3.mp3");
File srcFile4 = new File("f:/饿狼传说4.mp3");
File srcFile5 = new File("f:/饿狼传说5.mp3");
File destFile = new File("f:/饿狼传说全.mp3");
//建立数据通道
FileInputStream fileInputStream1 = new FileInputStream(srcFile1);
FileInputStream fileInputStream2 = new FileInputStream(srcFile2);
FileInputStream fileInputStream3 = new FileInputStream(srcFile3);
FileInputStream fileInputStream4 = new FileInputStream(srcFile4);
FileInputStream fileInputStream5 = new FileInputStream(srcFile5);
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
Vector<FileInputStream> list = new Vector<FileInputStream>();
list.add(fileInputStream1);
list.add(fileInputStream2);
list.add(fileInputStream3);
list.add(fileInputStream4);
list.add(fileInputStream5);
Enumeration<FileInputStream> elements = list.elements();
//建立序列流
SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
//建立小缓冲数组
byte[] buf = new byte[1024];
int length = 0;
int i = 0;
//写出数据
while((length = sequenceInputStream.read(buf)) != -1){
fileOutputStream.write(buf, 0, length);
}
//关闭资源
sequenceInputStream.close();
fileOutputStream.close();
}
public static void mergeFile2() throws IOException{
//找到目标文件
File dir = new File("f:/cut_merge_music");
File destFile = new File("f:/cut_merge_music/饿狼传说全.mp3");
//通过目标文件夹找到所有的MP3文件,然后把所有的MP3文件添加到vector中
Vector<FileInputStream> list = new Vector<FileInputStream>();
File[] listFiles = dir.listFiles();
for(File file : listFiles){
if(file.getName().endsWith(".mp3")){
list.add(new FileInputStream(file));
}
}
//通过Vector获取迭代器
Enumeration<FileInputStream> elements = list.elements();
//建立文件 的输出通道
FileOutputStream fileOutputStream = new FileOutputStream(destFile);
//建立序列流
SequenceInputStream sequenceInputStream = new SequenceInputStream(elements);
//建立小缓冲数组
byte[] buf = new byte[1024];
int length = 0;
int i = 0;
//写出数据
while((length = sequenceInputStream.read(buf)) != -1){
fileOutputStream.write(buf, 0, length);
}
//关闭资源
sequenceInputStream.close();
fileOutputStream.close();
}
}
0 0
- 序列流_SequenceInputStream类
- 序列流
- 序列流
- 序列流
- 管道流、BufferedReader类、对象序列化
- IO流/属性类/序列化
- IO流,序列化
- 转换流序列化
- 序列流 SequenceInputStream
- 对象流序列化
- IO序列输入输出流
- 【bzoj1283】 序列 费用流
- SequenceInputStream(序列流)
- javaday22-IO序列流
- 【bzoj1283】【序列】【费用流】
- 合并(序列)流
- 序列流 SequenceInputStream
- 输入输出流,序列化
- C++设计模式-Template Method
- 2017阿里前端测试题(div模拟下拉列表)
- angular2 HostListener
- linux下进程的信号量
- 思维导图:一张图了解jQuery
- 序列流_SequenceInputStream类
- 把Sublime Text3从windwos移到ubunut上
- 中文劲爆的音乐歌曲 有什么 国内重金属摇滚音乐,激情音乐,舞曲,电音
- 三天打鱼两天晒网(C语言)
- 进程间通信--命名管道
- POJ 1990 MooFest (树状数组)
- bzoj3143: [Hnoi2013]游走
- 对象输入输出流_ObjectInputStream类与ObjectOutputStream类
- L1-011. A-B