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)文章
記錄一次connection reset 錯(cuò)誤的解決全過(guò)程
這篇文章主要介紹了記錄一次connection reset 錯(cuò)誤的解決全過(guò)程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-04-04java連接MySQL數(shù)據(jù)庫(kù)的代碼
這篇文章主要為大家詳細(xì)介紹了java連接MySQL數(shù)據(jù)庫(kù)的相關(guān)代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-10-10spring boot空屬性賦值問(wèn)題與aspect日志實(shí)現(xiàn)方法
這篇文章主要介紹了spring boot空屬性賦值問(wèn)題與aspect日志實(shí)現(xiàn)方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08java前后端使用ajax數(shù)據(jù)交互問(wèn)題(簡(jiǎn)單demo)
這篇文章主要介紹了java前后端使用ajax數(shù)據(jù)交互問(wèn)題(簡(jiǎn)單demo),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。2023-06-06一文帶你掌握J(rèn)ava?SPI的原理和實(shí)踐
在Java中,我們經(jīng)常會(huì)提到面向接口編程,這樣減少了模塊之間的耦合,更加靈活,Java?SPI?(Service?Provider?Interface)就提供了這樣的機(jī)制,本文就來(lái)講講它的原理與具體使用吧2023-05-05java實(shí)現(xiàn)socket客戶端連接服務(wù)端
本文是個(gè)人剛剛開(kāi)始學(xué)習(xí)如何通過(guò)socket去發(fā)送信息下邊的案例,也是書(shū)上的在這留下筆記,最后附上一個(gè)實(shí)例,有需要的小伙伴可以參考下。2015-10-10