怎么用正则表达式来分析web日志,只要分析ip地址和页面访问时间
1条回答 默认 最新
- threenewbee 2018-11-24 05:31关注
你得贴出几行日志文件的内容才知道。
比如你一行是用逗号分隔的字段,那么都不要正则public static String[] toArrayByFileReader1(String name) { // 使用ArrayList来存储每行读取到的字符串 ArrayList<String> arrayList = new ArrayList<>(); try { FileReader fr = new FileReader(name); BufferedReader bf = new BufferedReader(fr); String str; // 按行读取字符串 while ((str = bf.readLine()) != null) { arrayList.add(str); } bf.close(); fr.close(); } catch (IOException e) { e.printStackTrace(); } // 对ArrayList中存储的字符串进行处理 int length = arrayList.size(); int[] array = new int[length]; for (int i = 0; i < length; i++) { String s = arrayList.get(i); array[i] = s; } // 返回数组 return array; }
String[] arr = toArrayByFileReader1(你的文件名);
for (String s: arr)
{
String[] all = s.split(","); //分割符是逗号
String ip = all[第几列是ip - 1];
String dt = all[第几列是访问时间 - 1];
}本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报