1.SequenceInputStream序列流:能将其他输入流的串联
用处:读完第一个再去读第二个输入流
用法:构造方法:SequenceInputStream(InputStream s1,InputStream s2):串联2个InputStream还可以,多个就很麻烦;
SequenceInputStream(Enumeration<? extends InputStream> e):Enumation对象需要通过Vector中获取,Vector是一个集合不过因为考虑线程安全效率比较低,他返回的的集合对象就是最初的Enumeration:
例子:将一个歌分成4份;再将4份合拼为1份
/** * 将一个文件切割为4份再将4份合拼为1份 * @author Administrator * */ public class sequenceInputStream { public static void main(String[] args) throws IOException { // TODO Auto-generated method stub //cutFile("C:/1.mp3","C:/mp3"); mergeFile("C:/mp3","C:/hepin.mp3"); } /** * 将文件以2M为一个单位分成多份 * @param pathFrom 被分文件位置 * @param pathTo 分成文件存放文件夹位置 * @throws IOException IO异常 */ public static void cutFile(String pathFrom,String pathTo) throws IOException{ File f=new File(pathFrom); FileInputStream fs=new FileInputStream(f); int length=0; byte[] buffer=new byte[1024*1024*2]; for(int i = 0 ; (length = fs.read(buffer))!=-1 ; i++){ FileOutputStream fos=new FileOutputStream(new File(pathTo,"part"+i+".mp3")); fos.write(buffer, 0, length); fos.close(); } fs.close(); } /** * 将一个文件夹中的多个文件合拼为一个文件 * @param pathFrom 存放多个文件的文件夹位置 * @param pathTo 合拼文件存放位置 * @throws IOException */ public static void mergeFile(String pathFrom,String pathTo) throws IOException{ File f=new File(pathFrom);//找到目标文件夹将其中的所有子文件都放到Vector中变成Enumation集合中去 File[] files=f.listFiles(); Vector<FileInputStream> vector=new Vector<FileInputStream>(); for(File file:files){ FileInputStream fs1=new FileInputStream(file); vector.add(fs1); } Enumeration<FileInputStream> e=vector.elements(); SequenceInputStream sis=new SequenceInputStream(e); FileOutputStream fos=new FileOutputStream(pathTo); int length=0; byte[] buffer=new byte[1024]; while((length=sis.read(buffer))!=-1){ fos.write(buffer,0,length); } fos.close(); sis.close(); } }