欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2023-12-17 23:18:10
复制代码 代码如下:package com.cjonline.foundation.evisa; import java.io.bufferedreader;import...

复制代码 代码如下:

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

上一篇:

下一篇: