一次日志请求次数统计

package  test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.Map;
import java.util.TreeMap;

public class Count {
    
    public static void main(String[] args) throws Exception {
        File file = new File("d:/kht_d2.log");
        BufferedReader in = new BufferedReader(
                new InputStreamReader(
                        new FileInputStream(file), "UTF-8"));
        Map<String, Integer> map = read(in);
        for(String str : map.keySet()) {
            System.out.println(str + "=" + map.get(str));
        }
        in.close();
    }
    
    public static Map<String, Integer> read(BufferedReader in) throws Exception {
        Map<String, Integer> map = new TreeMap<String, Integer>();
        String str = null;
        int count = 0;
        while((str = in.readLine()) != null) {
            if(str.length() > 20 && (str.indexOf("") != -1 )) {
                String date = str.substring(str.indexOf(":") + 1,
                        str.indexOf(":") + 11);
                if(map.get(date) == null) {
                    count = 0;
                    map.put(date, ++count);
                } else {
                    map.put(date, ++count);
                }
            }
        }
        return map;
    }
    
}

说明:

      这次是统计从我们平台发往其他平台的请求次数,思路就是对每一行的关键字进行验证,如果存在进行加1操作

原文地址:https://www.cnblogs.com/gaoguofeng/p/5850439.html