序列流_音频文件的切割与合并

public class SquenceStream {
public static void main(String[] args) throws Exception {
split(new File("F:/test/Beyond.mp3"),10, new File("F:/"));
System.out.println("文件切割完毕");
LinkedHashSet<FileInputStream> set = new LinkedHashSet<FileInputStream>();
set.add(new FileInputStream(new File("F:/part_1.mp3")));
set.add(new FileInputStream(new File("F:/part_2.mp3")));
set.add(new FileInputStream(new File("F:/part_3.mp3")));
set.add(new FileInputStream(new File("F:/part_4.mp3")));
combine(set, new File("F:/goal.mp3"));
System.out.println("文件合并完成");
}
/**
* 切割文件,切割份数,切割后保存路径
* @throws Exception 
*/
public static void split(File src,int num,File dir) throws Exception{
FileInputStream fis = new FileInputStream(src);
byte[] b = new byte[1024*300];
int len = 0;
FileOutputStream fos = null;
for (int i = 1; i <= num; i++) {
len = fis.read(b);
if (len != -1) {
fos = new FileOutputStream(dir + "part_" + i + ".mp3");
fos.write(b, 0, len);
}
fis.close();
fos.close();
}
/**
* 合并方法
* @throws Exception 
*/
public static void combine(LinkedHashSet<FileInputStream> set,File goal) throws Exception{
final Iterator<FileInputStream> it = set.iterator();
FileOutputStream fos = new FileOutputStream(goal);
SequenceInputStream seq = new SequenceInputStream(new Enumeration<InputStream>() {
public boolean hasMoreElements() {
return it.hasNext();
}
public InputStream nextElement() {
return it.next();
}
});
byte[] b = new byte[1024];
int len = 0;
while((len = seq.read(b)) != -1){
fos.write(b, 0, len);
}
fos.close();
seq.close();
}

}

备注:仅掌握序列流,为了体现核心代码,代码中未进行异常的处理。

你可能感兴趣的:(序列流_音频文件的切割与合并)