服务器之家

服务器之家 > 正文

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

时间:2019-10-13 14:48     来源/作者:java教程网

代码如下:


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;
    }
}        

 

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字
抖音撒撒累累是什么歌 撒撒累累张艺兴歌曲名字 2019-06-05
超A是什么意思 你好a表达的是什么
超A是什么意思 你好a表达的是什么 2019-06-06
返回顶部