-------android培训、java培训、期待与您交流! ----------
SequenceInputStream
表示其他输入流的逻辑串联。它从输入流的有序集合开始,并从第一个输入流开始读取,直到到达文件末尾,接着从第二个输入流读取,依次类推,直到到达包含的最后一个输入流的文件末尾为止。
文件的分割与合并:
public class SplitFile {
public static void main(String[] args) {
try {
// spitFile();
hebingFile();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void spitFile() throws IOException{
FileInputStream fis = new FileInputStream("c:\\1.mp3");
FileOutputStream fos;
<span style="white-space:pre"> </span>//对buf的长度限制为1M
byte[] buf = new byte[1024*1024];
int len = 0;
int count= 1;
while((len= fis.read(buf))!=-1){
<span style="white-space:pre"> </span>//读取文件buf长度的文件大小,新构造一个FileOutputStream,写到不同的文件下
fos = new FileOutputStream("c:\\splitFiles\\"+(count++)+".part");
fos.write(buf,0,len);
fos.close();
}
fis.close();
}
public static void hebingFile() throws IOException {
<span style="white-space:pre"> </span>//因为知道文件的个数是3个,所以这里就没有做循环
InputStream inputStream = new FileInputStream("c:\\splitFiles\\1.part");
InputStream inputStream2 = new FileInputStream("c:\\splitFiles\\2.part");
InputStream inputStream3 = new FileInputStream("c:\\splitFiles\\3.part");
ArrayList<InputStream> arrayList = new ArrayList<InputStream>();
arrayList.add(inputStream);
arrayList.add(inputStream2);
arrayList.add(inputStream3);
Iterator<InputStream> iterator = arrayList.iterator();
Enumeration<InputStream> enumeration = new Enumeration<InputStream>() {
@Override
public InputStream nextElement() {
// TODO Auto-generated method stub
return iterator.next();
}
@Override
public boolean hasMoreElements() {
// TODO Auto-generated method stub
return iterator.hasNext();
}
};
SequenceInputStream sequenceInputStream = new SequenceInputStream(enumeration);
OutputStream outputStream = new FileOutputStream("c:\\splitFiles\\1.mp3");
byte[] buf = new byte[1024];
int len = 0;
while((len=sequenceInputStream.read(buf))!=-1){
outputStream.write(buf,0,len);
}
sequenceInputStream.close();
outputStream.close();
}
}
文件的合并,利用了io流中的合并流,将多个文件合并成一个文件