序列流:
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
* 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();
}
}
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
* 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();
}
}