把一首MP3切成n份,每份是1m,然后再合并起来。

package cn.itcast.other;

 

import java.io.File;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.SequenceInputStream;

import java.util.Vector;

 


 public class Demo2 {

public static void main(String[] args) throws IOException {

// cutFile();

mergeFile();

}

//合并mp3文件

public static void mergeFile() throws IOException{

File dir= new File("f:\\music");

File[] files = dir.listFiles(); //获取到文件夹中的所有子文件

//创建一个Vector对象存储FileInputStream对象

Vector<FileInputStream> vector = new Vector<FileInputStream>();

//遍历数组,

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

if(files[i].getName().endsWith(".mp3")){

vector.add(new FileInputStream(files[i]));

}

}

//创建一个序列流对象

SequenceInputStream inputStream = new SequenceInputStream(vector.elements());

//创建一个输出流对象

FileOutputStream fileOutputStream = new  FileOutputStream("f:\\晚风.mp3");

//边读边写

byte[] buf = new byte[1024];

int length = 0; 

while((length = inputStream.read(buf))!=-1){

fileOutputStream.write(buf,0,length);

}

//关闭资源

fileOutputStream.close();

inputStream.close();

}

//切割mp3

public static void cutFile() throws IOException{

File file = new File("F:\\美女\\1.mp3");

FileInputStream fileInputStream = new FileInputStream(file);

byte[] buf = new byte[1024*1024];

int length = 0 ; 

int count = 1; 

while((length = fileInputStream.read(buf))!=-1){

//每读取一次,则生成一个文件

FileOutputStream fileOutputStream = new FileOutputStream("F:\\music\\part"+count+".mp3");

//把读取到的数据写出

fileOutputStream.write(buf,0,length);

count++;

//关闭资源

fileOutputStream.close();

}

//关闭资源

fileInputStream.close();

}

 

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值