java读取txt文件,使用正则表达式获取信息

废话不多说,直接上代码,主程序代码:

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:33redis中获取药品##ff4c978e-1dc4-4b28-a744-d3ed8aa725ea##耗时:15ms
11-24 11:05:33redis中获取药品##ff6a6b6a-7319-4006-8525-c9e2f96a1a71##耗时:33ms
11-24 11:05:33redis中获取药品##ff93cff9-7125-4394-9326-e6fdef8f0436##耗时:32ms
11-24 11:05:33redis中获取药品##ffc61566-7150-461e-97c8-2134092ab5ad##耗时:31ms
11-24 11:05:33redis中获取药品##ffca5bfc-965e-4cb0-b8a3-f287cdacf29b##耗时:20ms
11-24 11:05:33redis中获取药品##FFEE3495-09F3-465C-8EB0-EAD997E92C99##耗时:32ms
11-24 11:05:33redis中获取药品##FFFF8DC2-FFBC-4DE1-92EE-67BED80C7CED##耗时:30ms
11-24 11:05:33 **********在redis中获取规则##SelectDrug_Contraindication##耗时:50560ms*************
11-24 11:05:33 *****************************Usage************************************
11-24 11:05:33redis中获取药品##00618b04-f26a-4e70-b4c5-6c3510b68ac9##耗时:23ms
11-24 11:05:33redis中获取药品##009daa8f-3fef-4c68-b94b-113d7b2467f1##耗时:36ms
11-24 11:05:33redis中获取药品##00ed1b16-a2f1-4447-91b8-3f0055ee88d0##耗时:36ms
11-24 11:05:33redis中获取药品##01151b46-9951-4934-9247-9c1de4a14d11##耗时:38ms
11-24 11:05:33redis中获取药品##018472c5-a255-41c1-9151-474963fc7b0a##耗时:23ms
11-24 11:05:33redis中获取药品##01daa570-bd6c-491b-a988-bcedaafdd889##耗时:31ms
11-24 11:05:33redis中获取药品##01e8b0d4-e651-43eb-935c-7e6d3f649c12##耗时:32ms
11-24 11:05:34redis中获取药品##01ee3ec6-d6fc-4c16-94e1-73df56d4d9c8##耗时:34ms
11-24 11:05:34redis中获取药品##01fded77-b433-47d0-ae73-57fddbce8fd6##耗时:22ms
11-24 11:05:34redis中获取药品##021E93A8-C3A6-45EB-8562-662D3D960478##耗时:30ms
11-24 11:05:34redis中获取药品##022159ad-836e-4f1a-a509-a582da43b165##耗时:37ms
11-24 11:05:34redis中获取药品##0230c585-c94e-496b-992e-4935037e6ade##耗时:35ms
11-24 11:05:34redis中获取药品##024947a1-f762-42b3-99fe-bd8307db5056##耗时:19ms
11-24 11:05:34redis中获取药品##02abf1ce-aa2a-4741-945a-ff2b68a98820##耗时:34ms
11-24 11:05:34redis中获取药品##030b1784-2d1a-4da3-bcd8-0ff1c05df5db##耗时:37ms
11-24 11:05:34redis中获取药品##034d5ba0-1a85-44a4-ad39-b192f707a488##耗时:36ms
11-24 11:05:34redis中获取药品##035abc45-b31c-48b3-b5a1-7f989f66f982##耗时:36ms
11-24 11:05:34redis中获取药品##036b1558-9935-42b0-807c-ace55f5591e1##耗时:16ms
  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Java可以通过以下方法获取文件编码: 1. 使用第三方库:比较常用的是使用Apache Commons IO库中的BOMInputStream类,该类可以自动识别文件编码并去除BOM信息。 ```java File file = new File("test.txt"); BOMInputStream inputStream = new BOMInputStream(new FileInputStream(file), false); String charsetName = inputStream.getBOMCharsetName(); if (charsetName == null) { charsetName = "UTF-8"; // 默认编码 } InputStreamReader reader = new InputStreamReader(inputStream, charsetName); ``` 2. 判断文件的BOM信息:BOM是在Unicode文件开头的几个字节中放置的特殊标记,用于标识文件编码。可以通过读取文件的前几个字节来判断是否存在BOM信息。 ```java File file = new File("test.txt"); InputStream inputStream = new FileInputStream(file); byte[] bom = new byte[3]; inputStream.read(bom); // 读取文件前3个字节 if (bom[0] == -17 && bom[1] == -69 && bom[2] == -65) { charsetName = "UTF-8"; // UTF-8带BOM } else if (bom[0] == -2 && bom[1] == -1) { charsetName = "UTF-16BE"; // UTF-16BE带BOM } else if (bom[0] == -1 && bom[1] == -2) { charsetName = "UTF-16LE"; // UTF-16LE带BOM } else { charsetName = "UTF-8"; // 默认编码 } InputStreamReader reader = new InputStreamReader(inputStream, charsetName); ``` 需要注意的是,有些文件可能并不带BOM信息,此时需要根据文件内容自行判断编码。比较常用的方法是通过正则表达式判断其中的中文字符或者非ASCII字符,从而推断出文件编码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值