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;
}
}
您可能感兴趣的文章:
- Java 的 FileFilter文件过滤,readline读行操作
- Java 的 FileFilter文件过滤,readline读行操作
- JAVA操作properties文件的代码实例
- java file to inputstream以及New Document From InputStream读取和写入文件操作实例代码
- java 实现txt文件读取,写入操作实例代码。
- java通过InputStream读取和写入文件操作实例代码
- java操作html转换成pdf文件的实例代码
- java文件的重命名与移动操作实例代码
- java文件操作代码片断实例实现统计文件中字母出现的个数功能
- 数据库操作:java连接ORACLE实例代码演示
- java.io操作实例代码
- 过滤html 文件java 代码记录
- Java操作文件(夹)的几断代码
- 数据库操作:java连接ORACLE实例代码演示
- 低级文件操作实例代码
- 转:Java, jsp, web app 获得文件路径的代码实例
- 详解JAVA I/O之二——Java IO 多种方式读文件的代码实例
- 数据库操作:java连接DB2实例代码演示
- 数据库操作:java连接MSSQL实例代码演示
- 数据库操作:java连接MYSQL实例代码演示