The example of this article describes the method of mp3 merging in Java. Share it with everyone for your reference. The specific implementation method is as follows:
Copy the 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 & file name
List<String> list = new ArrayList<String>();
File dir = new File("D:/aaa"); // Directory to be traversed, use recursion to get files. If there are too many files, it will be over.
recursion(dir.listFiles(), list); // Recursive function
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);
}
public static void recursion(File[] fs, List<String> list) // Recursively obtain .mp3 files in all 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 the 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 be helpful to everyone’s Java programming.