Java 的 FileFilter文件过滤与readline读行操作实例代码

 更新时间:2013年09月09日 14:58:58   作者:  
这篇文章介绍了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;
    }
}       

相关文章

  • 浅谈Spring注入模型

    浅谈Spring注入模型

    如果不深入到Spring的源码,是很少有机会了解到Spring的注入模型(AutowireMode)。但是为了扫清我们学习Spring源码的障碍,我们有必要了解下Spring的注入模型,感兴趣的同学可以阅读一下
    2023-04-04
  • 教你bat脚本一键配置java开发环境

    教你bat脚本一键配置java开发环境

    公司新入职一名员工,项目经理让我安排新人工作,首先需要对java开发相关环境进行配置安装,但时常会因为安装配置不到位或者操作错误导致时间的浪费,所以在空余时间收集了一系列软件的免安装版本,感兴趣的朋友一起看看吧
    2021-12-12
  • Java中Json与List、Map、entity的互相转化

    Java中Json与List、Map、entity的互相转化

    在开发中,Json转换的场景往往也就是那么几个,本文主要介绍了Java中Json与List、Map、entity的互相转化,具有一定的参考价值,感兴趣的可以了解一下
    2022-07-07
  • java使用BeanUtils.copyProperties方法对象复制同名字段类型不同赋值为空问题解决方案

    java使用BeanUtils.copyProperties方法对象复制同名字段类型不同赋值为空问题解决方案

    这篇文章主要给大家介绍了关于java使用BeanUtils.copyProperties方法对象复制同名字段类型不同赋值为空问题的解决方案,文中通过代码介绍的非常详细,对大家的学习或者工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2023-11-11
  • Spring Security中防护CSRF功能详解

    Spring Security中防护CSRF功能详解

    这篇文章主要介绍了Spring Security中防护CSRF功能,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2023-01-01
  • Spring Boot 实现Restful webservice服务端示例代码

    Spring Boot 实现Restful webservice服务端示例代码

    这篇文章主要介绍了Spring Boot 实现Restful webservice服务端示例代码,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-11-11
  • Spring Boot整合logback一个简单的日志集成架构

    Spring Boot整合logback一个简单的日志集成架构

    今天小编就为大家分享一篇关于Spring Boot整合logback一个简单的日志集成架构,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2019-01-01
  • Java中的这些骚操作你不能不知道!!!

    Java中的这些骚操作你不能不知道!!!

    今天在看python相关的东西,看到各种骚操作,回头想了下Java有没有什么骚操作,整理下面几种,一起看一下吧,希望能给你带来帮助
    2021-07-07
  • Java进阶之FileUpload完成上传的实例

    Java进阶之FileUpload完成上传的实例

    这篇文章主要介绍了 Java进阶之FileUpload完成上传的实例的相关资料,希望通过本文能帮助到大家,需要的朋友可以参考下
    2017-09-09
  • Java多线程模式之Balking模式详解

    Java多线程模式之Balking模式详解

    这篇文章主要介绍了Java多线程模式之Balking模式,结合实例形式较为详细的分析了Balking模式的原理、用法与相关注意事项,需要的朋友可以参考下
    2017-06-06

最新评论