IO 文件拆分

package com.zyf.day22;

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.Enumeration;
import java.util.Vector;

//把一首mp3切成n份
public class demo2 {
    public static void main(String[] args) throws IOException {
    	mergeFile();
	}
    //合并
    public static void mergeFile() throws IOException{
    	//找到目标文件
    	File dir = new File("c:\\mp3");
    	Vector<FileInputStream> vector = new Vector<FileInputStream>();
    	File[] files = dir.listFiles();
    	for(File file: files){
    		if(file.getName().endsWith(".mp3")){
    			vector.add(new FileInputStream(file));
    		}
    	}
    	//通过Vector获取迭代器
    	Enumeration<FileInputStream> e = vector.elements();
    	//创建序列流
    	SequenceInputStream inputStream = new SequenceInputStream(e);
    	FileOutputStream fileOutputStream = new FileOutputStream("c:\\mp3\\合并.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("c:\\mp3\\Adele - Rolling In The Deep.mp3");
    	
    	File dir = new File("c:\\mp3");
    	
    	FileInputStream fileInputStream = new FileInputStream(file);
    	
    	byte[] buf = new byte[1024 * 1024];
    	int length = 0;
    	for(int i=0;(length = fileInputStream.read(buf))!= -1;i++){
    		FileOutputStream fileOutputStream = new FileOutputStream(new File(dir,"part" + i + ".mp3"));
    		fileOutputStream.write(buf,0,length);
    		fileOutputStream.close();
    	}
    	//关闭资源
    	fileInputStream.close();
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值