zoukankan      html  css  js  c++  java
  • Java:大文件拆分工具

    java大文件拆分工具(过滤掉表头)

    import java.io.BufferedReader;
    import java.io.BufferedWriter;
    import java.io.FileReader;
    import java.io.FileWriter;
    import java.io.IOException;
    
    public class SplitTools {
        public static void main(String[] args) throws IOException {
            // "E:\eutranrelation.csv";
            String bigFilePath = args[0];
            // "E:\eutranrelation-%s.csv";
            String splitFileParttern = args[1];
            // 32
            int splitFileCount = Integer.valueOf(args[2]);
    
            BufferedReader reader = new BufferedReader(new FileReader(bigFilePath));
            String header = reader.readLine();
            int totalLine = 0;
            if (header != null) {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    totalLine++;
                }
            }
    
            reader.close();
    
            int splitFileTotalLines = totalLine / splitFileCount;
            int generateFileIdx = 0;
    
            BufferedWriter bufferedWriter = null;
            reader = new BufferedReader(new FileReader(bigFilePath));
            header = reader.readLine();
            int currentIdx = 0;
            if (header != null) {
                String line = null;
                while ((line = reader.readLine()) != null) {
                    if (currentIdx == 0) {
                        String filePath = String.format(splitFileParttern,
                                String.valueOf(generateFileIdx));
                        bufferedWriter = new BufferedWriter(
                                new FileWriter(filePath));
                    }
    
                    bufferedWriter.write(line + "
    ");
    
                    currentIdx++;
    
                    if (currentIdx == splitFileTotalLines) {
                        generateFileIdx++;
                        if (generateFileIdx != splitFileCount) {
                            currentIdx = 0;
                            bufferedWriter.flush();
                            bufferedWriter.close();
                        }
                    }
                }
    
                if (bufferedWriter != null) {
                    bufferedWriter.flush();
                    bufferedWriter.close();
                }
            }
    
            reader.close();
    
            System.out.println("The total number of documents is:" + totalLine);
        }
    }
  • 相关阅读:
    语法专题:错误处理机制
    语法专题:数据类型的转换
    运算符:其他运算符,运算顺序
    运算符:二进制位运算符
    运算符:布尔运算符
    运算符:比较运算符
    day09 继承、super、this、抽象类
    day08 String类、Static关键字、Arrays类、Math类
    day06 Scanner类、Random类、匿名对象、ArrayList类
    day05 encapsulation
  • 原文地址:https://www.cnblogs.com/yy3b2007com/p/9808805.html
Copyright © 2011-2022 走看看