IO 文件拆分

来源:互联网 发布:ubuntu 安装mysql5.7 编辑:程序博客网 时间:2024/03/29 14:33
package com.zyf.day22;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.Enumeration;import java.util.Vector;//把一首mp3切成n份public class demo2 {    public static void main(String[] args) throws IOException {    mergeFile();}    //合并    public static void mergeFile() throws IOException{    //找到目标文件    File dir = new File("c:\\mp3");    Vector<FileInputStream> vector = new Vector<FileInputStream>();    File[] files = dir.listFiles();    for(File file: files){    if(file.getName().endsWith(".mp3")){    vector.add(new FileInputStream(file));    }    }    //通过Vector获取迭代器    Enumeration<FileInputStream> e = vector.elements();    //创建序列流    SequenceInputStream inputStream = new SequenceInputStream(e);    FileOutputStream fileOutputStream = new FileOutputStream("c:\\mp3\\合并.mp3");    //建立缓冲数组读取文件    byte[] buf = new byte[1024];    int length = 0;    while((length = inputStream.read(buf))!=-1){    fileOutputStream.write(buf,0,length);    }    //关闭资源    fileOutputStream.close();    inputStream.close();    }        //切割mp3    public static void cutFile() throws IOException{    File file = new File("c:\\mp3\\Adele - Rolling In The Deep.mp3");        File dir = new File("c:\\mp3");        FileInputStream fileInputStream = new FileInputStream(file);        byte[] buf = new byte[1024 * 1024];    int length = 0;    for(int i=0;(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();    }}


                                             
0 0
原创粉丝点击