API
来把我的博客中的文字转成音频,.pcm
文件,这是个二进制文件,好了,废话就说到这,下面介绍如何把多个二进制文件合并为一个二进制文件.
复制文件的算法如下:
多个源文件合并成一个目标文件,算法跟复制文件差不多,算法描述如下:
// 缓存数组
byte[] buffer = new byte[2048];
// 每次读入的字节数量
int inSize = -1;
// 批量读入字节到buffer缓存中,并返回读入的自己数量给inSize
// 这里的in为输入流对象
while ((inSize = in.read(buffer)) != -1)
{
// 把buffer缓存中的字节写入输出流(也就是目标文件)
// 这里的out为输出流对象
out.write(buffer, 0, inSize);
}
/**
* 合并文件ArrayList中的多个源文件为一个文件.
*
* @param targetFilePath
* 目标文件路径名称字符串.
* @param sourceFilePathList
* 存放源文件的路径名称字符串的ArrayList集合.
*/
public static void merge2TargetFileDeleteSourceFile(String targetFilePath,
ArrayList<String> sourceFilePathList)
{
// 如果ArrayList中有东西
if (sourceFilePathList.size() > 0)
{
BufferedInputStream in;
try (BufferedOutputStream out = new BufferedOutputStream(
new FileOutputStream(new File(targetFilePath)));)
{
System.out.println("源文件列表:");
for (Iterator<String> iterator = sourceFilePathList
.iterator(); iterator.hasNext();)
{
String sourceFilePath = iterator.next();
System.out.println(" " + sourceFilePath);
File sourceFile = new File(sourceFilePath);
in = new BufferedInputStream(
new FileInputStream(sourceFile));
// 缓存数组
byte[] buffer = new byte[2048];
// 每次读入的字节数量
int inSize = -1;
// 批量读入字节到buffer缓存中,并返回读入的自己数量给inSize
while ((inSize = in.read(buffer)) != -1)
{
// 把buffer缓存中的字节写入输出流(也就是目标文件)
out.write(buffer, 0, inSize);
}
// 关闭源文件
in.close();
// 删除这个源文件
sourceFile.delete();
}
} catch (FileNotFoundException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
}
}