java读取txt文件,使用正则表达式获取信息
来源:互联网 发布:手机淘宝能分期付款吗 编辑:程序博客网 时间:2024/06/05 05:36
废话不多说,直接上代码,主程序代码:
import java.io.*;import java.util.ArrayList;import java.util.List;import java.util.regex.Matcher;import java.util.regex.Pattern;public class GetTimeFromLoggerTxt{ /** * 获取日志文件中的时间 * @throws FileNotFoundException */ public static void main(String[]args) throws FileNotFoundException { { //匹配次数 int matchTime = 0; //存匹配上的字符串 List<String> strs = new ArrayList<>(); try { //编码格式 String encoding = "UTF-8"; //文件路径 File file = new File("F:\\svn\\work\\Redis.txt"); if (file.isFile() && file.exists()){ // 判断文件是否存在 //输入流 InputStreamReader read = new InputStreamReader( new FileInputStream(file), encoding);// 考虑到编码格 BufferedReader bufferedReader = new BufferedReader(read); String lineTxt = null; //读取一行 while ((lineTxt = bufferedReader.readLine()) != null) { //正则表达式 matchTime = getMatchTime(matchTime, strs, lineTxt); } read.close(); } else { System.out.println("找不到指定的文件"); } } catch (Exception e) { System.out.println("读取文件内容出错"); e.printStackTrace(); } List<Integer> nums = getSum(strs); double avg = getAvgTime(nums,matchTime); System.out.print(avg); } } private static int getMatchTime(int matchTime, List<String> strs, String lineTxt) { Pattern p = Pattern.compile("[0-9]*ms$"); Matcher m = p.matcher(lineTxt); boolean result = m.find(); String find_result = null; if (result) { matchTime++; find_result = m.group(0); strs.add(find_result); } return matchTime; } private static List<Integer> getSum(List<String> strs) { List<Integer> nums = new ArrayList<>(); for(String str : strs){ String s = str.replace("ms",""); Integer a = Integer.valueOf(s); nums.add(a); } return nums; } private static double getAvgTime(List<Integer> nums, int matchTime) { double sum = 0; double avg ; for(Integer num : nums){ sum+=num; } avg = sum/matchTime; return avg; }}
Redis.txt文件如下:
11-24 11:05:33 在redis中获取药品##ff4c978e-1dc4-4b28-a744-d3ed8aa725ea##耗时:15ms11-24 11:05:33 在redis中获取药品##ff6a6b6a-7319-4006-8525-c9e2f96a1a71##耗时:33ms11-24 11:05:33 在redis中获取药品##ff93cff9-7125-4394-9326-e6fdef8f0436##耗时:32ms11-24 11:05:33 在redis中获取药品##ffc61566-7150-461e-97c8-2134092ab5ad##耗时:31ms11-24 11:05:33 在redis中获取药品##ffca5bfc-965e-4cb0-b8a3-f287cdacf29b##耗时:20ms11-24 11:05:33 在redis中获取药品##FFEE3495-09F3-465C-8EB0-EAD997E92C99##耗时:32ms11-24 11:05:33 在redis中获取药品##FFFF8DC2-FFBC-4DE1-92EE-67BED80C7CED##耗时:30ms11-24 11:05:33 **********在redis中获取规则##SelectDrug_Contraindication##耗时:50560ms*************11-24 11:05:33 *****************************Usage************************************11-24 11:05:33 在redis中获取药品##00618b04-f26a-4e70-b4c5-6c3510b68ac9##耗时:23ms11-24 11:05:33 在redis中获取药品##009daa8f-3fef-4c68-b94b-113d7b2467f1##耗时:36ms11-24 11:05:33 在redis中获取药品##00ed1b16-a2f1-4447-91b8-3f0055ee88d0##耗时:36ms11-24 11:05:33 在redis中获取药品##01151b46-9951-4934-9247-9c1de4a14d11##耗时:38ms11-24 11:05:33 在redis中获取药品##018472c5-a255-41c1-9151-474963fc7b0a##耗时:23ms11-24 11:05:33 在redis中获取药品##01daa570-bd6c-491b-a988-bcedaafdd889##耗时:31ms11-24 11:05:33 在redis中获取药品##01e8b0d4-e651-43eb-935c-7e6d3f649c12##耗时:32ms11-24 11:05:34 在redis中获取药品##01ee3ec6-d6fc-4c16-94e1-73df56d4d9c8##耗时:34ms11-24 11:05:34 在redis中获取药品##01fded77-b433-47d0-ae73-57fddbce8fd6##耗时:22ms11-24 11:05:34 在redis中获取药品##021E93A8-C3A6-45EB-8562-662D3D960478##耗时:30ms11-24 11:05:34 在redis中获取药品##022159ad-836e-4f1a-a509-a582da43b165##耗时:37ms11-24 11:05:34 在redis中获取药品##0230c585-c94e-496b-992e-4935037e6ade##耗时:35ms11-24 11:05:34 在redis中获取药品##024947a1-f762-42b3-99fe-bd8307db5056##耗时:19ms11-24 11:05:34 在redis中获取药品##02abf1ce-aa2a-4741-945a-ff2b68a98820##耗时:34ms11-24 11:05:34 在redis中获取药品##030b1784-2d1a-4da3-bcd8-0ff1c05df5db##耗时:37ms11-24 11:05:34 在redis中获取药品##034d5ba0-1a85-44a4-ad39-b192f707a488##耗时:36ms11-24 11:05:34 在redis中获取药品##035abc45-b31c-48b3-b5a1-7f989f66f982##耗时:36ms11-24 11:05:34 在redis中获取药品##036b1558-9935-42b0-807c-ace55f5591e1##耗时:16ms
阅读全文
0 0
- java读取txt文件,使用正则表达式获取信息
- 读取txt 文件信息
- java读取txt文件,获取行数
- 使用java读取txt里边的文件内容并获取大小(M).txt
- JAVA读取txt文件
- Java读取txt文件
- java读取txt文件
- java读取txt文件
- Java读取txt文件
- java读取txt文件
- java 读取txt文件
- java读取txt文件
- JAVA读取txt文件
- java读取txt文件
- java读取txt文件
- Java读取txt文件
- java--正则表达式的应用:读取文件,获取其中的电话号码
- JAVA读取不同编码的txt文件,java获取txt文件编码格式
- 什么是多线程 菜鸟入门
- my.cnf 简单的配置文件
- 前端框架
- Java中关于try、catch、finally的总结
- 在ROS中实现PING线程
- java读取txt文件,使用正则表达式获取信息
- matlab中卷积运算conv2的三种形式
- 如何通俗解释Docker是什么?
- Shiro-密码的MD5加密
- cfB. Beautiful Divisors
- oj第六周作业题解
- android获取网页数据的几种方式
- css字体属性
- 利用哈希表 复杂数组去重