This article describes the Java implementation of MP3 merging method. Share to everyone for your reference. The implementation methods are as follows:
Copy Code code as follows:
Package test;
Import java.io.*;
Import java.util.*;
public class Test6
{
public static void Main (string[] args) throws Exception
{
String s = "D:/out.mp3"; Output Directory & filename
list<string> list = new arraylist<string> ();
File dir = new file ("D:/aaa"); To traverse the directory, use recursion to get the file, too many files will be finished.
Recursion (Dir.listfiles (), list); Recursive functions
string[] ss = new string[list.size ()];
for (int i = 0; i < ss.length; i++)
{
Ss[i] = List.get (i);
}
System.out.println ();
Combine (s, SS);
}
The. mp3 file of the public static void recursion (file[] FS, list<string> list)//recursively subdirectories.
{
for (File F:fs)
{
if (F.isdirectory ())
{
Recursion (F.listfiles (), list);
}
Else
{
if (F.getname (). EndsWith (". mp3"))
{
List.add (F.getabsolutepath ());
}
}
}
}
Private static Boolean combine (String outfile, string[] infiles) throws Exception
{
File out = new file (outfile);
file[] files = new File[infiles.length];
for (int i = 0; i < files.length; i++)
{
Files[i] = new File (infiles[i]);
}
FileInputStream FIS = null;
FileOutputStream fos = new FileOutputStream (outfile, true); Merging is actually a continuation of the file, written as True
for (int i = 0; i < files.length; i++)
{
FIS = new FileInputStream (files[i]);
int len = 0;
For (byte[] buf = new byte[1024 * 1024]; (len = Fis.read (buf))!=-1;)
{
Fos.write (buf, 0, Len);
}
}
Fis.close ();
Fos.close ();
return true;
}
}
I hope this article will help you with your Java programming.