将大文件数据拆分成多个文件

package com.lay.util;

import org.springframework.util.StringUtils;

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

/**
 * 拆分文件
 */
public class ListUtils {
    /**
     * 将一组数据固定分组,每组n个元素
     * @param source 要分组的数据源
     * @param n      每组n个元素
     * @param <T>
     * @return
     */
    public static <T> List<List<T>> fixedGrouping(List<T> source, int n) {

        if (null == source || source.size() == 0 || n <= 0)
            return null;
        List<List<T>> result = new ArrayList<List<T>>();

        int sourceSize = source.size();
        int size = (source.size() / n) + 1;
        for (int i = 0; i < size; i++) {
            List<T> subset = new ArrayList<T>();
            for (int j = i * n; j < (i + 1) * n; j++) {
                if (j < sourceSize) {
                    subset.add(source.get(j));
                }
            }
            result.add(subset);
        }
        return result;
    }




    /**
     * 读取并拆分文件
     * @param originalFileName 原始文件的名字 eg:  data.csv
     * @param hoName 文件文件名用于新文件名拼接 eg: data_1.csv
     * @param newFilePath 多文件放入的路径
     * @param len 一个文件存放的数据
     * @return
     * @throws IOException
     */
    public static List<String> readAndSplitFile(String originalFileName,String hoName,String newFilePath, int len) throws IOException {
        long startTime = System.currentTimeMillis();    //获取开始时间
        //1.读取CSV文件数据
        BufferedReader br = new BufferedReader(
                new InputStreamReader(new FileInputStream(originalFileName), "UTF-8")
        );

        String line;
        List<String> li = new ArrayList<String>();
        while ((line = br.readLine()) != null) {
            //读取获取CSV文件行、列数据
            String[] info = line.split(",");
                li.add(line);
        }

        List<List<String>> lists = ListUtils.fixedGrouping(li, len);
        int fileNumber = lists.size();

        // 存放新生成的文件路径
        List<String> newFilePathList = new ArrayList<>();
        for (int i = 0; i < fileNumber; i++) {
            List<String> list = lists.get(i);
            String fileName = "";
            // 生成新文件的路径
            if (!StringUtils.isEmpty(newFilePath)){
                File dir = new File(newFilePath);
                if (!dir.exists()) {// 判断目录是否存在
                    dir.mkdir();
                }
                fileName = new StringBuilder(newFilePath).append(hoName+"_"+(i + 1)).append("."+originalFileName.split("\\.")[1]).toString();
            }else {
                int index = originalFileName.lastIndexOf("/");
                newFilePath = originalFileName.substring(0,index+1);
                fileName = new StringBuilder(newFilePath).append(hoName+"_"+(i + 1)).append("."+originalFileName.split("\\.")[1]).toString();
            }
            boolean exportCsv = exportCsv(new File(fileName), list);
            if (exportCsv && i == fileNumber - 1) {
                System.out.println("文件--" + (i * len) + "个-" + (list.size() + i * len) + "个" + fileName + "--生成成功!");
                newFilePathList.add(fileName);
            } else if (exportCsv && i != fileNumber - 1) {
                System.out.println("文件--" + (i * len) + "个-" + ((i + 1) * len) + "个" + fileName + "--生成成功!");
                newFilePathList.add(fileName);
            } else {
                System.out.println("文件--" + fileName + "--生成失败!");
            }
        }
        long endTime = System.currentTimeMillis();    //获取结束时间
        double userTime = ((double) endTime - startTime) / 1000;//执行时长<秒>
        System.out.println("\n" + String.format("执行完毕,共花时长:%s秒", userTime));//秒
        return newFilePathList;
    }

    /**
     * 导出(到CSV文件)
     *
     * @param file     csv文件(路径+文件名),csv文件不存在会自动创建
     * @param dataList 数据
     * @return
     */
    public static boolean exportCsv(File file, List<String> dataList) {
        boolean isSucess = false;

        FileOutputStream out = null;
        OutputStreamWriter osw = null;
        BufferedWriter bw = null;
        try {
            out = new FileOutputStream(file);
            osw = new OutputStreamWriter(out, "UTF-8");//设置输出编码方式为UTF-8
            //加上UTF-8文件的标识字符
            osw.write(new String(new byte[]{(byte) 0xEF, (byte) 0xBB, (byte) 0xBF}));

            bw = new BufferedWriter(osw);
            if (dataList != null && !dataList.isEmpty()) {
                for (String data : dataList) {
                    bw.append(data).append("\r");
                }
            }
            isSucess = true;
        } catch (Exception e) {
            isSucess = false;
        } finally {
            if (bw != null) {
                try {
                    bw.close();
                    bw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (osw != null) {
                try {
                    osw.close();
                    osw = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (out != null) {
                try {
                    out.close();
                    out = null;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

        return isSucess;
    }


    public static void main(String[] args) throws IOException {
        List<String> result1 = readAndSplitFile("E:\\ELK搜索引擎\\iot_grp_member_20220526.CSV","iot_grp_member_20220526","E:\\ELK搜索引擎\\多个文件\\", 10000);
        System.out.println("------------------指定新生成的文件路径如下:------------");
        result1.stream().forEach(item->{
            System.out.println(item);
        });
        System.out.println("------------------指定新生成的文件路径完毕------------");
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值