这篇文章介绍了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;
}
}
上一篇:深入理解final变量的初始化下一篇:Java 开发的几个注意点总结 热门搜索:
java文件操作
过滤操作
java实例
过滤的操作要点
相关文章
Java 的 FileFilter文件过滤与readline读行操作实例代码
2021-09-27阅读(3976)评论(0)推荐()这篇文章介绍了Java 的 FileFilter文件过滤与readline读行操作实例代码,有需要的朋友可以参考一下
java核心编程之文件过滤类FileFilter和FilenameFilter
2021-10-06阅读(5365)评论(0)推荐()这篇文章主要为大家详细介绍了java文件过滤类FileFilter和FilenameFilter,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
java io读取文件操作代码实例
2021-10-18阅读(7410)评论(0)推荐()这篇文章主要介绍了java io读取文件操作代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
Java实现的文件过滤代码分享(按后辍过滤)
2021-10-18阅读(7633)评论(0)推荐()这篇文章主要介绍了Java实现的文件过滤代码分享,本文通过后辍名过滤,代码写简洁,容易看懂,需要的朋友可以参考下
Node.js readline 逐行读取、写入文件内容的示例
2021-11-12阅读(5393)评论(0)推荐()本篇文章主要介绍了Node.js readline逐行读取、写入文件内容的示例,运用readline逐行读取的两种实现,小编觉得挺不错的,现在分享给大家,也给大...
java文件的简单读写操作方法实例分析
2021-09-19阅读(8321)评论(0)推荐()这篇文章主要介绍了java文件的简单读写操作方法,结合实例形式分析了java文件流进行读写操作的方法与相关操作注意事项,需要的朋友可以参考下
Java对xls文件进行读写操作示例代码
2021-09-10阅读(3407)评论(0)推荐()Java开发项目中经常会碰到处理Excel文件中数据的情况,下面这篇文章主要给大家介绍了利用Java对xls文件进行读写操作的相关资料,文中通过示例代码介绍的非...
取消
提交评论