java SequenceInputStream练习

需求: 把一首MP3切成n份,每份是1m,然后再合并起来。

public class Demo2 {
    
    public static void main(String[] args) throws IOException {
//      cutFile();
        mergeFile();
    }
    
    
    //合并mp3文件
    public static void mergeFile() throws IOException{
        File dir= new File("f:\\music");
        File[] files = dir.listFiles(); //获取到文件夹中的所有子文件
        //创建一个Vector对象存储FileInputStream对象
        Vector<FileInputStream> vector = new Vector<FileInputStream>();
        //遍历数组,
        for(int i = 0; i<files.length ; i++){
            if(files[i].getName().endsWith(".mp3")){
                vector.add(new FileInputStream(files[i]));
            }
        }
        
        //创建一个序列流对象
        SequenceInputStream inputStream = new SequenceInputStream(vector.elements());
        //创建一个输出流对象
        FileOutputStream fileOutputStream = new  FileOutputStream("f:\\晚风.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("F:\\美女\\1.mp3");
        FileInputStream fileInputStream = new FileInputStream(file);
        byte[] buf = new byte[1024*1024];
        int length = 0 ; 
        int count = 1; 
        
        while((length = fileInputStream.read(buf))!=-1){            
            
            //每读取一次,则生成一个文件
            FileOutputStream fileOutputStream = new FileOutputStream("F:\\music\\part"+count+".mp3");
            //把读取到的数据写出
            fileOutputStream.write(buf,0,length);
            count++;
            //关闭资源
            fileOutputStream.close();
        }
        //关闭资源
        fileInputStream.close();    
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 要在计算机内播放或是处理音频文件,也就是要对声音文件进行数、模转换,这个过程同样由采样和量化构成,人耳所能听到的声...
    Viking_Den阅读 10,368评论 1 10
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 173,612评论 25 708
  • 不知不觉四周年了啊!我还记得第一次看见你们是在快乐大本营的下周预告里。看见三个如诗一样的少年。那一刻你们突然就撞...
    故橙阅读 153评论 0 1
  • 我曾经历过一次巨大的迷茫,称之为巨大真不是夸夸其谈,那是我考上研究生后经历的一次空前的迷茫,那些日子,整天像丢了魂...
    解忧少年阅读 616评论 24 9
  • 我怎么会取了一个这么无聊的名字?今天周四后天就要考试了,还是梳理一下思路吧! 目前,我的论文还需要更熟悉。 然后我...
    丑桔不上火阅读 313评论 0 0