码迷,mamicode.com
首页 > 编程语言 > 详细

合并mp3, vector,Enumeration,SequenceInputStream,FileFilter,匿名内部类.

时间:2014-10-25 20:07:48      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   os   ar   java   for   sp   

 1 package test;
 2 
 3 import java.io.*;
 4 import java.util.*;
 5 
 6 public class Test20
 7 {
 8     public static void main(String[] args) throws Exception
 9     {
10         // 从给定目录 搜索MP3
11         File dir = new File("D:/aaa");
12         List<File> list = new ArrayList<File>();
13         FileFilter filefilter = new FileFilter()
14         {
15             public boolean accept(File pathname)
16             {
17                 if (pathname.getName().endsWith(".mp3"))
18                     return true;
19                 if (pathname.isDirectory())
20                     return true;
21                 else
22                     return false;
23             }
24         };
25         method(dir.listFiles(filefilter), list, filefilter);//list里装mp3
26         doSequence(list);//合并 MP3
27     }
28     private static void doSequence(List<File> list) throws Exception
29     {
30         Vector<InputStream> vector = new Vector<InputStream>();
31         for (File f : list)
32         {
33             vector.addElement(new FileInputStream(f));
34         }
35         Enumeration<InputStream> e = vector.elements();
36         SequenceInputStream sis = new SequenceInputStream(e);
37         BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:/hebing.mp3"));
38         int len = 0;
39         for (byte[] buf = new byte[1024 * 1024]; (len = sis.read(buf)) != -1;)
40         {
41             bos.write(buf, 0, len);
42             bos.flush();
43         }
44         sis.close(); // 这个相当于 3个 fis的close();
45         bos.close();
46     }
47     public static void method(File[] files, List<File> list, FileFilter filefilter)
48     {
49         for (File f : files)
50         {
51             if (f.isDirectory())
52                 method(f.listFiles(filefilter), list, filefilter);
53             else
54                 list.add(f);
55         }
56     }
57 }

 

合并mp3, vector,Enumeration,SequenceInputStream,FileFilter,匿名内部类.

标签:style   blog   color   io   os   ar   java   for   sp   

原文地址:http://www.cnblogs.com/stone1022/p/4050759.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!