Java 的 FileFilter文件过滤,readline读行操作

package com.cjonline.foundation.evisa;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigDecimal;

public class Test {
    
    public static void main(String[] args) throws Exception {
        //文件过滤器,文件路径可以使用D:\\pressTest\\test绝对路径,也可以用src/test。
        File[] files = new File("src").listFiles(new FileFilter() {
            public boolean accept(File arg0) {
                if(arg0.getName().endsWith(".txt")){//选择txt文件
                    return true;
                }
                return false;
            }
        });
        FileInputStream is =null;    //输入流读取文件
        BufferedReader dr =null;    //读行
        for (File file : files) {
            System.out.println("---------【 file name : "+ file.getName() +"】----------");
            is =new FileInputStream(file);
            dr=new BufferedReader(new InputStreamReader(is)); 
            String[] strings = new String[]{"Total transferred:","Requests per second:","[ms] (mean)","Time per request:",
                    "Transfer rate:","Failed requests:","Write errors:"};
            BigDecimal[] BigDecimals = calPress(dr);
            int i=0;
            for (BigDecimal BigDecimal : BigDecimals) {
                System.out.println(strings[i]+"        "+BigDecimal);
                i++;
            }
            System.out.println();
        }
        dr.close();
        is.close();
    }

    private static BigDecimal[] calPress(BufferedReader dr)
            throws IOException {
        BigDecimal[] res = new BigDecimal[]{BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO
                ,BigDecimal.ZERO,BigDecimal.ZERO,BigDecimal.ZERO} ;
        String totalTrans;
        while((totalTrans = dr.readLine()) != null){
            if (totalTrans.startsWith("Total transferred:")) {
                String[] st = totalTrans.split(" ");
                BigDecimal value = BigDecimal.valueOf(Double.valueOf(st[st.length-2]));
                res[0]=res[0].add(value);
            }
            if (totalTrans.startsWith("Requests per second:")) {
                String[] st = totalTrans.split(" ");
                BigDecimal value = BigDecimal.valueOf(Double.valueOf(st[st.length-3]));
                res[1]=res[1].add(value);
            }
            if (totalTrans.endsWith("[ms] (mean)")) {
                String[] st = totalTrans.split(" ");
                BigDecimal value = BigDecimal.valueOf(Double.valueOf(st[st.length-3]));
                res[2]=res[2].add(value);
            }
            if (totalTrans.startsWith("Time per request:") && !totalTrans.endsWith("[ms] (mean)")) {
                String[] st = totalTrans.split(" ");
                BigDecimal value = BigDecimal.valueOf(Double.valueOf(st[st.length-7]));
                res[3]=res[3].add(value);
            }
            if (totalTrans.startsWith("Transfer rate:")) {
                String[] st = totalTrans.split(" ");
                BigDecimal value = BigDecimal.valueOf(Double.valueOf(st[st.length-3]));
                res[4]=res[4].add(value);
            }
            if(totalTrans.startsWith("Failed requests:")){
                String[] st = totalTrans.split(" ");
                BigDecimal value = BigDecimal.valueOf(Double.valueOf(st[st.length-1]));
                res[5]=res[5].add(value);
            }
            if(totalTrans.startsWith("Write errors:")){
                String[] st = totalTrans.split(" ");
                BigDecimal value = BigDecimal.valueOf(Double.valueOf(st[st.length-1]));
                res[6]=res[6].add(value);
            }

        }
        return res;
    }
}

 

转载于:https://www.cnblogs.com/lbangel/p/3309831.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值