加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java大文件的分割和合并

发布时间:2020-12-15 03:19:22 所属栏目:Java 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.Filen

以下代码由PHP站长网 52php.cn收集自互联网

现在PHP站长网小编把它分享给大家,仅供参考

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.SequenceInputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
 
/**
 * SplitFile.java
 * @author:<a href="mailto:[email?protected]">bylv</a>
 * @DATE:2015-9-7  上午09:27:43
 *  Copyright (C) 2011 ISoftStone
 */
 
/**
 * 功能说明:
 * 
 * @author: <a href="mailto:[email?protected]">bylv</a>
 * @DATE:2015-9-7 @TIME: 上午09:27:43
 */
public class SplitFile {
 
    private static final int SIZE = 1024 * 1024;// 定义单个文件的大小这里采用1m
 
    /**
     * 功能说明:
     * 
     * @param args
     * @author: <a href="mailto:[email?protected]">bylv</a>
     * @throws Exception
     * @DATE:2015-9-7 @TIME: 上午09:27:44
     */
    public static void main(String[] args) throws Exception {
        // 拆分
        // File file = new File("D:/无标题.bmp");
        // splitFile(file);
        // 合并
        File file = new File("C:/parfiles");
        mergeFile(file);
    }
 
    /**
     * 功能说明:合并文件
     * 
     * @param file
     * @author: <a href="mailto:[email?protected]">bylv</a>
     * @throws Exception
     * @throws FileNotFoundException
     * @DATE:2015-9-7 @TIME: 上午09:47:31
     */
    private static void mergeFile(File dir) throws Exception {
        // 读取properties文件的拆分信息
        File[] files = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir,String name) {
                return name.endsWith(".properties");
            }
        });
        File file = files[0];
        // 获取该文件的信息
        Properties pro = new Properties();
        FileInputStream fis = new FileInputStream(file);
        pro.load(fis);
        String fileName = pro.getProperty("fileName");
        int splitCount = Integer.valueOf(pro.getProperty("partCount"));
        if (files.length != 1) {
            throw new Exception(dir + ",该目录下没有解析的properties文件或不唯一");
        }
 
        // 获取该目录下所有的碎片文件
        File[] partFiles = dir.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir,String name) {
                return name.endsWith(".part");
            }
        });
        // 将碎片文件存入到集合中
        List<FileInputStream> al = new ArrayList<FileInputStream>();
        for (int i = 0; i < splitCount; i++) {
            try {
                al.add(new FileInputStream(partFiles[i]));
            } catch (Exception e) {
                // 异常
                e.printStackTrace();
            }
        }
        try {
            // 构建文件流集合
            Enumeration<FileInputStream> en = Collections.enumeration(al);
            // 将多个流合成序列流
            SequenceInputStream sis = new SequenceInputStream(en);
            FileOutputStream fos = new FileOutputStream(new File(dir,fileName));
            byte[] b = new byte[1024];
            int len = 0;
            while ((len = sis.read(b)) != -1) {
                fos.write(b,len);
            }
            fos.close();
            sis.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
    /**
     * 功能说明:拆分文件
     * 
     * @param file
     * @author: <a href="mailto:[email?protected]">bylv</a>
     * @DATE:2015-9-7 @TIME: 上午09:28:58
     */
    private static void splitFile(File file) {
        try {
            FileInputStream fs = new FileInputStream(file);
            // 定义缓冲区
            byte[] b = new byte[SIZE];
            FileOutputStream fo = null;
            int len = 0;
            int count = 0;
 
            /**
             * 切割文件时,记录 切割文件的名称和切割的子文件个数以方便合并
             * 这个信息为了简单描述,使用键值对的方式,用到了properties对象
             */
            Properties pro = new Properties();
            // 定义输出的文件夹路径
            File dir = new File("C:/parfiles");
            // 判断文件夹是否存在,不存在则创建
            if (!dir.exists()) {
                dir.mkdirs();
            }
            // 切割文件
            while ((len = fs.read(b)) != -1) {
                fo = new FileOutputStream(new File(dir,(count++) + ".part"));
                fo.write(b,len);
                fo.close();
            }
            // 将被切割的文件信息保存到properties中
            pro.setProperty("partCount",count + "");
            pro.setProperty("fileName",file.getName());
            fo = new FileOutputStream(new File(dir,(count++) + ".properties"));
            // 写入properties文件
            pro.store(fo,"save file info");
            fo.close();
            fs.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
 
    }
 
}

以上内容由PHP站长网【52php.cn】收集整理供大家参考研究

如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读