代码分析,笔写

 

背景后面再交代了呵呵!

import java.io.*;
import java.util.*;

//12G日志做到分析,并获取相应记录
public class TestFile {
    public static void main(String[] args) throws IOException {
        File file = new File("");
        FileReader reader = new FileReader("");
        splitFileLineCount("");
        //最后统一刷一把到内存
        for (String s : map.keySet()) {
            writeFile(s, map.get(s));
        }
    }

    public int getCount(String name){
        return countMap.get(name);
    }

    public void print(String name) throws IOException {
        LineNumberReader reader = new LineNumberReader(new FileReader(name));
        String re="";
        while (( re= reader.readLine())!=null){
            System.out.println(re);
        }
    }

    public static int splitFileLineCount(String filename) {
        int cnt = 0;
        LineNumberReader reader = null;
        try {
            reader = new LineNumberReader(new FileReader(filename));
            @SuppressWarnings("unused")
            String lineRead = "";
            while ((lineRead = reader.readLine()) != null) {
                int length = lineRead.indexOf(":");
                String name = lineRead.substring(0, length);
                insertLength(name, lineRead);
            }
            cnt = reader.getLineNumber();
        } catch (Exception ex) {
            cnt = -1;
            ex.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
        return cnt;
    }

    static HashMap<String, List> map = new HashMap();
    static HashMap<String, Integer> countMap = new HashMap<String, Integer>()

    private static void insertLength(String name, String lineRead) throws IOException {
        List<String> ad = map.get(name);
        if (ad == null) ad = new LinkedList<>();
        ad.add(lineRead);
        map.put(name, ad);
        if (countMap.get(name) == null) {
            countMap.put(name, 0);
        }
        int count = countMap.get(name);
        countMap.put(name, count++);

        //毕竟是12G的文件还是
        if (ad.size() > 100) ;
        writeFile(name, ad);
    }

    private static synchronized void writeFile(String name, List<String> ad) throws IOException {
        FileWriter fileWriter = new FileWriter(name);
        for (String s : ad) {
            fileWriter.write(s);
        }
    }


}

除特别注明外,本站所有文章均为duzhi原创,转载请注明出处来自https://www.duzhi.me/article/4203.html

联系我们

******

在线咨询:点击这里给我发消息

邮件:ashang.peng#aliyun.com

QR code