mp 3合併

8277 ワード

 1 package test;

 2 

 3 import java.io.*;

 4 import java.util.*;

 5 

 6 public class Test6

 7 {

 8     public static void main(String[] args) throws Exception

 9     {

10         String s = "D:/out.mp3"; //   &  

11         List<String> list = new ArrayList<String>();

12         File dir = new File("D:/aaa"); //  ,  , .

13         recursion(dir.listFiles(), list); //  

14         String[] ss = new String[list.size()];

15         for (int i = 0; i < ss.length; i++)

16         {

17             ss[i] = list.get(i);

18         }

19         System.out.println();

20         combine(s, ss);

21     }

22     public static void recursion(File[] fs, List<String> list) //  .mp3 .

23     {

24         for (File f : fs)

25         {

26             if (f.isDirectory())

27             {

28                 recursion(f.listFiles(), list);

29             }

30             else

31             {

32                 if (f.getName().endsWith(".mp3"))

33                 {

34                     list.add(f.getAbsolutePath());

35                 }

36             }

37         }

38     }

39     private static boolean combine(String outFile, String[] inFiles) throws Exception

40     {

41         File out = new File(outFile);

42         File[] files = new File[inFiles.length];

43         for (int i = 0; i < files.length; i++)

44         {

45             files[i] = new File(inFiles[i]);

46         }

47         FileInputStream fis = null;

48         FileOutputStream fos = new FileOutputStream(outFile, true); //  , true

49         for (int i = 0; i < files.length; i++)

50         {

51             fis = new FileInputStream(files[i]);

52             int len = 0;

53             for (byte[] buf = new byte[1024 * 1024]; (len = fis.read(buf)) != -1;)

54             {

55                 fos.write(buf, 0, len);

56             }

57         }

58         fis.close();

59         fos.close();

60         return true;

61     }

62 }