IO-文件切割和合并。

java IO文件的切割和合并。

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/6/12.
 */
public class Demo {
    //文件分割
    public static List<String> cut(String src,int cutCount){
        List<String> nameList = new ArrayList<String>();
        File file = new File(src);
        byte[] buf = new byte[(int)file.length() / cutCount+1];//加1的原因是有时候除不尽有余数。
        InputStream is = null;
        BufferedInputStream bis = null;
        try {
            is = new FileInputStream(file);
            bis = new BufferedInputStream(is);
            int len = -1;
            int number = 1;
            while ((len=bis.read(buf))!=-1){
                String smallFileName = "e:/" + file.getName().substring(0, file.getName().indexOf(".")) + "-" + number + ".bak";
                OutputStream os = new FileOutputStream(smallFileName);
                nameList.add(smallFileName);
                BufferedOutputStream bos = new BufferedOutputStream(os);
                bos.write(buf,0,len);
                bos.close();
                os.close();
                number++;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return nameList;
    }
    //文件合并
    public static void merge(List<String> list, String newName) {
        OutputStream os = null;
        BufferedOutputStream bos = null;
        try {
            os = new FileOutputStream(newName);
            bos = new BufferedOutputStream(os);
            int count = list.size();
            for(int i=0;i<count;i++){
                File file = new File(list.get(i));
                byte[] buf = new byte[(int) file.length()];
                InputStream is = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(is);
                bis.read(buf);
                bos.write(buf);
                bis.close();
                is.close();
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                bos.close();
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }

    public static void main(String[] args){
        String str = "E:/英雄时刻/英雄时刻_20160604-00点11分36s.avi";
        List<String> nameList=cut(str,3);
        merge(nameList,"E:/精彩视频.avi");
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值