zoukankan      html  css  js  c++  java
  • JAVA-分割大文件(按行分割)

    package com.testSplitByNumber;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.nio.charset.Charset;
    import java.util.HashMap;
    import java.util.Map;
    
    import com.csvreader.CsvReader;
    
    public class SplitFile {
        public static void main(String args[]){
            String path = "F:/zh.csv";
            String folder = path.substring(0, path.lastIndexOf("."));
            File file = new File(path);
            File file2 = new File(folder);
            Map<String,BufferedWriter> map = new HashMap<String, BufferedWriter>();
            if(! file.exists()){
                System.out.println("文件不存在!");
            }else {
                if(! file2.exists() || !file2.isDirectory()){
                    file2.mkdir();
                }
                long start = System.currentTimeMillis();
                CsvReader reader = null ;
                BufferedWriter bw = null;
                try {
                    reader = new CsvReader(path, ',', Charset.forName("utf-8"));
                    reader.readHeaders();
                    int count = 0;
                    while(reader.readRecord()){
                        count ++;
                        String strs[] = reader.getValues();
                        String line = strs[strs.length - 1];
                        String fileName = folder + File.separator + file.getName().subSequence(0, file.getName().lastIndexOf("."))+ line.charAt(line.length() - 1)+".csv";
                        bw = map.get(fileName);
                        if(bw == null){
                            bw = new BufferedWriter(new FileWriter(new File(fileName)));
                        }
                        map.put(fileName, bw);
                        bw.write(writeLine(strs)+"
    ");
                        if(count % 100000 == 0){
                            bw.flush();
                            System.out.println("已经处理:" + count + "条");
                        }
                    }
                    long end = System.currentTimeMillis();
                    System.out.println("共用时:" + (end - start)/1000 + "s");
                } catch (IOException e) {
                    e.printStackTrace();
                }finally{
                    if(reader != null){
                        reader.close();
                        reader = null;
                    }
                    for(BufferedWriter bww : map.values()){
                        try {
                            bww.flush();
                            bww.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
        
        public static String writeLine(String strs[]){
            StringBuffer sb = new StringBuffer("");
            for(int i = 0;i < strs.length;i++){
                strs[i] = strs[i].replaceAll(",", ",");
                strs[i] = strs[i].replaceAll("\s*", "");
                sb.append(strs[i] + ",");
            }
            return sb.substring(0, sb.length() - 1).toString();
        }
    }
  • 相关阅读:
    post请求返回404
    启动网关服务报错 Unable to find GatewayFilterFactory with name RequestRateLimiter
    数据库远程连接linux报错2003 can't connect to MySQL server on ip (0) 防火墙没有开放端口3306
    idea下maven项目打包部署到tomcat服务器
    修改Idea背景色
    20种源代码测试工具
    作为一名测试工程师,需要具备哪些能力
    Android自动化测试工具——monkey简介及入门
    appium关于定位元素
    appium testcase2
  • 原文地址:https://www.cnblogs.com/zqzdong/p/4840517.html
Copyright © 2011-2022 走看看