Java 实现文件分割,合并功能

转载自:http://blog.csdn.net/qq_35101189/article/category/6444732/3

当初看到这个博主写的文件分割,感觉很有意思,后面自己稍微改进了下,程序:

package common;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * @Description 文件分割合并操作
 * @author           <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
 * @date         2016-12-22上午10:02:01
 */

public class FileSliptUtil {

    /**
     *
     * @Description 文件分割
     * @author        <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
     * @date        <p style="color:#000;font-family:微软雅黑;font-size=16px;">2016-12-22上午10:02:16</p>
     * @param src    分割文件路径
     * @param m        大小    
     * @throws IOException
     */
    public static void splitFileDemo(File src, int m) throws IOException {
        if(src.isFile()) {
            //获取文件的总长度
            long l = src.length();
            //获取文件名
            String fileName = src.getName().substring(0, src.getName().indexOf("."));
            //获取文件后缀
            String endName = src.getName().substring(src.getName().lastIndexOf("."));
            System.out.println(endName);
            InputStream in = null;
            try {
                in = new FileInputStream(src);
                for(int i = 1; i <= m; i++) {
                    StringBuffer sb = new StringBuffer();
                    sb.append(src.getParent());
                    sb.append("\\");
                    sb.append(fileName);
                    sb.append("_data");
                    sb.append(i);
                    sb.append(endName);
                    System.out.println(sb.toString());
                    
                    File file2 = new File(sb.toString());
                    //创建写文件的输出流
                    OutputStream out = new FileOutputStream(file2);
                    int len = -1;
                    byte[] bytes = new byte[10*1024*1024];
                    while((len = in.read(bytes))!=-1) {
                        out.write(bytes, 0, len);
                        if(file2.length() > (l / m)) {
                            break;
                        }
                    }
                    out.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(in != null)
                    in.close();
            }
            System.out.println("--- 文件分割完成 ---");
        }
    }

/*    *//**
     *
     * @Description 文件合并的方法 原版合并方法
     * @author        <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
     * @date        <p style="color:#000;font-family:微软雅黑;font-size=16px;">2016-12-22上午10:01:26</p>
     * @param src    合并文件路径
     *//*
    public static void joinFileDemo(String... src) {
        for(int i = 0; i < src.length; i++) {
            File file = new File(src[i]);
            String fileName = file.getName().substring(0, file.getName().indexOf("_"));
            String endName = file.getName().substring(file.getName().lastIndexOf("."));

            StringBuffer sb = new StringBuffer();
            sb.append(file.getParent());
            sb.append("\\");
            sb.append(fileName);
            sb.append(endName);
            System.out.println(sb.toString());
            try {
                //读取小文件的输入流
                InputStream in = new FileInputStream(file);
                //写入大文件的输出流
                File file2 = new File(sb.toString());
                OutputStream out = new FileOutputStream(file2,true);
                int len = -1;
                byte[] bytes = new byte[10*1024*1024];
                while((len = in.read(bytes))!=-1) {
                    out.write(bytes, 0, len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("文件合并完成!");
    }*/
    
    /**
     *
     * @Description 文件合并的方法 改进后的方法
     * @author        <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
     * @date        <p style="color:#000;font-family:微软雅黑;font-size=16px;">2016-12-22上午10:01:26</p>
     * @param src    合并文件路径
     */
    public static void joinFileDemo(String[] src) {
        //    获取合并文件
        File newFile = new File(src[0].toString());
        //    获取文件名    后缀
        String fileName = newFile.getName().substring(0, newFile.getName().indexOf("_"));
        String endName = newFile.getName().substring(newFile.getName().lastIndexOf("."));
        //    得到新的文件名
        StringBuffer sb = new StringBuffer();
        sb.append(newFile.getParent());
        sb.append("\\");
        sb.append(fileName);
        sb.append(endName);
        newFile = new File(sb.toString());
        for(int i = 0; i < src.length; i++) {
            File file = new File(src[i]);
            try {
                //读取小文件的输入流
                InputStream in = new FileInputStream(file);
                OutputStream out = new FileOutputStream(newFile, true);
                int len = -1;
                byte[] bytes = new byte[10*1024*1024];
                while((len = in.read(bytes))!=-1) {
                    out.write(bytes, 0, len);
                }
                out.close();
                in.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        System.out.println("文件合并完成!");
    }
}

package test;

import java.io.File;

import common.FileSliptUtil;

/**
 *
 * @Description 分割文件测试
 * @author           <p style="color:#8e8e8e;font-family:微软雅黑;font-size=16px;font-weight:bold;">Cloud</p>
 * @date         2017-1-5下午2:32:10
 */

public class TestFileSlipt {

    public static void main(String[] args) throws Exception {
        //分割文件
        FileSliptUtil.splitFileDemo(new File("e://jdk//jdk 1.8.rar"), 2);
        //合并文件
        //FileSliptUtil.joinFileDemo(new String[]{"C://Linux//jdk//jdk-7u76-linux-x64_data1.gz", "C://Linux//jdk-7u76-linux-x64_data2.gz", "C://Linux//jdk-7u76-linux-x64_data3.gz"});
    }
}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值