package com.io.properties;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.SequenceInputStream;
import java.util.Enumeration;
import java.util.Vector;
public class SplitFile {
public static void main(String[] args) {
try {
//split();
heBing();
} catch (IOException e) {
e.printStackTrace();
}
}
/*
* 合并多个文件成一个文件
*/
public static void heBing() throws IOException{
Vector<FileInputStream> v=new Vector<FileInputStream>();
v.add(new FileInputStream("g:/java/1.part"));
v.add(new FileInputStream("g:/java/2.part"));
v.add(new FileInputStream("g:/java/3.part"));
v.add(new FileInputStream("g:/java/4.part"));
Enumeration<FileInputStream>en=v.elements();
SequenceInputStream sis=new SequenceInputStream(en);
int num=0;
FileOutputStream fos=new FileOutputStream("g:/java/hebing.mp3");
byte[]b=new byte[1024];
while(-1!=(num=sis.read(b))){
fos.write(b,0,num);
fos.flush();
}
fos.close();
sis.close();
}
/*
* 切割文件,把一个文件按指定大小进行切割,分割成多个文件
*/
public static void split() throws IOException{
FileInputStream fis=new FileInputStream(new File("g:/java/a.mp3"));
FileOutputStream fos=null;
byte[]b=new byte[1024*1024*5];//大小是5M
int num=0;
int count=1;
while(-1!=(num=fis.read(b))){
fos=new FileOutputStream(new File("g:/java/"+ count++ +".part"));
fos.write(b,0,num);
fos.close();
}
fis.close();
}
}