程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> 關於JAVA >> Java 的 FileFilter文件過濾與readline讀行操作實例代碼

Java 的 FileFilter文件過濾與readline讀行操作實例代碼

編輯:關於JAVA

Java 的 FileFilter文件過濾與readline讀行操作實例代碼。本站提示廣大學習愛好者:(Java 的 FileFilter文件過濾與readline讀行操作實例代碼)文章只能為提供參考,不一定能成為您想要的結果。以下是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;
    }
}       

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved