读取大文件的方法这里有三种,
第一种,使用commons-io的FileUtils的类进行读取
第二种,使用Scanner进行读取
第三种,使用cache进行读取
读取文件大小:102M
使用commons-io的FileUtils类进行读取
public static void testReadFile() {
try {
LineIterator lineIterator = FileUtils.lineIterator(new File("D:/test.log"), "UTF-8");
while (lineIterator.hasNext()) {
String line = lineIterator.nextLine();
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
}
}
读取时间在8秒左右
使用Scanner进行读取:
public static void testScannerReadFile() {
FileInputStream fileInputStream = null;
Scanner scanner = null;
try {
fileInputStream = new FileInputStream("D:/test.log");
scanner = new Scanner(fileInputStream, "UTF-8");
while (scanner.hasNext()) {
String line = scanner.nextLine();
System.out.println(line);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
if (fileInputStream != null) {
try {
fileInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (scanner != null) {
scanner.close();
}
}
}
读取时间在10秒左右
使用cache读取
public static void readCache() {
String filename = "D:/test.log";
File file = new File(filename);
BufferedReader reader = null;
try {
reader = new BufferedReader(new FileReader(file), 10 * 1024 * 1024); //读大文件 设置缓存
String tempString = null;
while ((tempString = reader.readLine()) != null) {
System.out.println(tempString);
}
reader.close();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
读取时间在8秒左右,与commons-io的FileUtils不相上下,我这边暂时没有更大的文件进行比较如果,有更大的文件,欢迎进行测试比较。
读取大文件性能测试
发表于:2017-01-09
作者:网络转载
来源:
- 周排行
- 月排行
- 评论排行
-   云测试的两种类型——云与性能测试
-   性能测试场景设计深度解析
-   一次完整的性能测试,测试人员需要做...
-   Docker中IO、MariaDB的性能测试
-   怎样设计全链路压力测试平台?
-   性能测试准入准出规范
-   当我们讨论性能测试时,我们在说什么?
-   应用中间件性能测试技术方法
-   Docker容器网络性能测试和调优策略
-   系统吞吐量(TPS)和用户并发量计算公式
-   云测试的两种类型——云与性能测试
-   白盒测试的六种覆盖准则
-   怎样设计全链路压力测试平台?
-   性能测试准入准出规范
-   从软件出发,非功能测试思考总结
-   展望2017,谁是网络技术头牌?
-   C/C++单元测试工具Visual Unit 4 介绍
-   国内外最好用的6款Bug跟踪管理系统
-   性能测试指标评估必读
-   百度软件测试方案模板
-   38张史上最全的IT工程师技能图谱
-   C/C++单元测试工具Visual Unit 4 介绍
-   2019年,你会选哪些安全测试工具?
-   十款Web服务器性能压力测试工具
-   软件测试入门指南:周期、模型和文档化
-   记自动化测试成神学习之路
-   软件测试全景图 -最全的思维导图