Java 的 FileFilter文件過(guò)濾與readline讀行操作實(shí)例代碼
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 {
//文件過(guò)濾器,文件路徑可以使用D:\\pressTest\\test絕對(duì)路徑,也可以用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;
}
}
相關(guān)文章
Java縮略圖生成庫(kù)之Thumbnailator應(yīng)用說(shuō)明
Thumbnailator是一個(gè)為Java界面更流暢的縮略圖生成庫(kù),從API提供現(xiàn)有的圖像文件和圖像對(duì)象的縮略圖中簡(jiǎn)化了縮略過(guò)程,兩三行代碼就能夠從現(xiàn)有圖片生成縮略圖,使用起來(lái)非常方便,需要的朋友可以了解下2012-12-12
SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼
這篇文章主要介紹了SpringBoot+MyBatis簡(jiǎn)單數(shù)據(jù)訪問(wèn)應(yīng)用的實(shí)例代碼,需要的朋友可以參考下2017-05-05
基于Spring@Autowired注解與自動(dòng)裝配詳談
下面小編就為大家?guī)?lái)一篇基于Spring@Autowired注解與自動(dòng)裝配詳談。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
如何使用Spring AOP預(yù)處理Controller的參數(shù)
這篇文章主要介紹了如何使用Spring AOP預(yù)處理Controller的參數(shù)操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-08-08
java操作excel導(dǎo)入導(dǎo)出的3種方式
項(xiàng)目需要,要實(shí)現(xiàn)一個(gè)導(dǎo)入導(dǎo)出excel的功能,于是任務(wù)驅(qū)動(dòng)著我學(xué)習(xí)到了POI、easypoi和easyexcel這3個(gè)java操作Excel的工具,下面這篇文章主要給大家介紹了關(guān)于java操作excel導(dǎo)入導(dǎo)出的3種方式,需要的朋友可以參考下2023-05-05
Spring編程式和聲明式事務(wù)實(shí)例講解小結(jié)
這篇文章主要介紹了Spring編程式和聲明式事務(wù)實(shí)例講解小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-07-07

