黑马程序员技术交流社区

标题: 现学现用,切割MP3(制作铃声)和合并MP3(串烧) [打印本页]

作者: 潜力良驹    时间: 2015-7-25 21:18
标题: 现学现用,切割MP3(制作铃声)和合并MP3(串烧)
今天刚学的一点点序列流,用来实现切割和合并东西的,切割可以用来做铃声制作哟~合并可以用来做串烧~


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.SequenceInputStream;
import java.util.Vector;

//切割MP3与合并MP3(串烧)
//要求要1兆切割
public class Demo3 {
        public static void main(String[] args) throws Exception {
                cutFile();
        }
        public static void cutFile() throws Exception{
                //找到要切的文件
                File file = new File("G:\\after.mp3");
                //指定存放切好的文件路径
                File dir = new File("G:\\1");
                //建读取通道
                FileInputStream fileInputStream = new FileInputStream(file);
                //读取MP3
                byte[] buf = new byte[1024*1024];//按1兆切割,可以改变哒
                //记录切割次数
                long count = 0;
                //要先判断文件的大小,搞好是整兆的就不用+1了.
                if(file.length()%(1024*1024)!=0){
                        count = (file.length()/(1024*1024)+1);
                }else{
                        count = (file.length()%(1024*1024));
                }
                //用一个变量记录一次写入的大小
                int length = 0;
                //循环写入文件
                for(int i =0; i<count; i++){
                        //每次写入1兆
                        length = fileInputStream.read(buf);
                        //建写入通道
                        FileOutputStream fileOutputStream = new FileOutputStream(new File(dir,i+".mp3"));
                        fileOutputStream.write(buf, 0, length);
                        //关闭写入通道
                        fileOutputStream.close();
                }
                //关闭读取通道
                fileInputStream.close();
        }
        public static void magerFeil() throws Exception{
                //找到切割后的碎片目录
                File dir = new File("G:\\1");
                //找到目录下的所有子文件
                File[] files = dir.listFiles();
                //创建一个FileInputStream类的Vector对象
                Vector<FileInputStream> v = new Vector<FileInputStream>();
                //把所有的输入流对象添加到Vector中
                for(File file: files){
                        //要过滤
                        if(file.getName().endsWith(".mp3")){
                                //类型是FileInputStream的匿名对象
                                v.add(new FileInputStream(file));
                        }
                }
                //创建序列流对象,SequenceInputStream本身没有读写功能,但是继承了Serializable接口就能读取啦.
                SequenceInputStream inputStream = new SequenceInputStream(v.elements());
                //创建输出流对象
                FileOutputStream fileOutputStream = new FileOutputStream("G:\\after.mp3");
                //边读边写
                byte[] buf = new byte[1024];
                int length = 0;
                while((length = inputStream.read(buf))!=-1){
                        fileOutputStream.write(buf, 0, length);
                }
                fileOutputStream.close();
                inputStream.close();
        }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2