用纯java分析网站后台日志并按照IP排序
package byzx.fq.get;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;
import java.util.Map.Entry;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 用java程序读取日志并分析Ip访问量排序显示前十
* 读取=>提取(切片)=>统计=>排序=>显示
*/
public class IPget {
@SuppressWarnings("static-access")
// 主方法-IPget,通过word统计和sort组合。
// 两个参数,一个是日志路径,一个是IP提取生成文档
public static void main(String[] args) {
IPget ip = new IPget();
ip.getIp("i:/1.txt", "i:/2.txt");
}
public static void getIp(String filePath, String fileTo) {
try {
// 设置日志的编码格式,有利于数据读取
String encoding = "utf-8";
File file = new File(filePath);
// 判断文件是否存在,并创建流对象读取文件
if (file.isFile() && file.exists()) {
InputStreamReader read = new InputStreamReader(new FileInputStream(file), encoding);// 考虑到编码格式
BufferedReader bufferedReader = new BufferedReader(read);
// 设置读取字符字段与匹配IP的集合
String lineTxt = null;
List<String> list = new ArrayList<String>();
while ((lineTxt = bufferedReader.readLine()) != null) {
// System.out.println(lineTxt);
// 正则表达式读取IP地址
String regexString = "\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}";
// 进行相应的IP提取匹配
Pattern p = Pattern.compile(regexString);
Matcher m = p.matcher(lineTxt);
boolean result = m.find();
while (result) {
list.add(m.group().trim());
// System.out.println(m.group());
// System.out.println(set.size());
result = m.find();
// System.out.println(set.size());
}
}
// 创建IP读取存储文本文件,并写出
FileWriter fileWriter = new FileWriter(fileTo);
for (String string : list) {
fileWriter.write(string + " ");
}
// 关闭流对象,并调用ip处理方法
fileWriter.flush();
fileWriter.close();
word(fileTo);
read.close();
} else {
System.out.println("找不到指定的文件");
}
} catch (Exception e) {
System.out.println("读取文件内容出错");
e.printStackTrace();
}
}
// 针对已经提取的Ip进行相应的个数统计,参数为Ip提取的文档地址
public static void word(String filePath) {
// 设置统计个数和其他函数需要的变量
int wordCount = 0;
int n;
String s = " ";
FileInputStream f1 = null;
// 用于统计各个单词的个数,排序
Map<String, Integer> map = new HashMap<String, Integer>();
try {
File file = new File(filePath);
if (file.isFile() && file.exists()) { // 判断文件是否存在
f1 = new FileInputStream(file);
//设置字符读取的缓存大小
byte[] b = new byte[1024];
while ((n = f1.read(b)) != -1) {
String p = new String(b, 0, n);
//将读取的分片IP转化为String类型
s = s + p;
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
//关闭文件传输流
if (f1 != null) {
try {
f1.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 这个类会将字符串分解成一个个的标记
StringTokenizer token = new StringTokenizer(s);
while (token.hasMoreTokens()) { // 循环遍历
wordCount++;
String word = token.nextToken(" ");
// 按照空格进行分割
if (map.containsKey(word)) {
// HashMap不允许重复的key,所以利用这个特性,去统计ip的个数
int count = map.get(word);
map.put(word, count + 1);
// 如果HashMap已有这个ip,则设置它的数量加1
} else
map.put(word, 1);
// 如果没有这个ip,则新填入,数量为1
}
System.out.println("总共IP:" + wordCount);
sort(map); // 调用排序的方法,排序并输出!
}
//针对统计出来的IP结果进行相应的排序和处理,参数为ip的map统计结果
public static void sort(Map<String, Integer> map) {
//设置
List<Map.Entry<String, Integer>> infoIds = new ArrayList<Map.Entry<String, Integer>>(map.entrySet());
Collections.sort(infoIds, new Comparator<Map.Entry<String, Integer>>() {
public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
return (o2.getValue() - o1.getValue());
}
}); // 排序
// 全部输出为 infoIds.size()
for (int i = 0; i < 10; i++) { // 输出相应数量的排名和IP情况
Entry<String, Integer> id = infoIds.get(i);
System.out.println(id.getKey() + " : " + id.getValue());
}
}
}