java FileI(O)nputStream为什么比BufferedI(O)utputStream慢?
因为buffered多了一个缓冲区,读和写都是先把硬盘或者内存中的数据放到内存中一块缓存区域,到一定大小读写到硬盘或者内存
package io;
import java.io.*;
public class FileIOTest {
/**
* @param args
* @throws FileNotFoundException
*/
public static void main(String[] args) throws Exception {
//有buff的File***Stream
System.out.println("有buff的File***Stream耗时:");
new TimeTest() {
void run()throws Exception{
FileInputStream fis = new FileInputStream("F:\\Test\\file.zip");
FileOutputStream fos = new FileOutputStream("F:\\Test\\file1.zip");
byte[] buf = new byte[1024];
int length = 0;
while ((length = fis.read(buf)) > 0) {
fos.write(buf, 0, length);
}
fis.close();
fos.close();
}
}.getTime();
//有buff的Buffered***Stream
System.out.println("有buff的Buffered***Stream耗时:");
new TimeTest() {
void run()throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\Test\\file.zip"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("F:\\Test\\file2.zip"));
byte[] buf = new byte[1024];
int length = 0;
while ((length = bis.read(buf)) > 0) {
bos.write(buf, 0, length);
}
bis.close();
bos.close();
}
}.getTime();
//无buff的File***Stream
System.out.println("无buff的File***Stream耗时:");
new TimeTest() {
void run()throws Exception{
FileInputStream fis = new FileInputStream("F:\\Test\\file.zip");
FileOutputStream fos = new FileOutputStream("F:\\Test\\file3.zip");
int data = 0;
while ((data = fis.read()) !=-1) {
fos.write(data);
}
fis.close();
fos.close();
}
}.getTime();
//无buff的Buffered***Stream
System.out.println("无buff的Buffered***Stream耗时:");
new TimeTest() {
void run()throws Exception{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("F:\\Test\\file.zip"));
BufferedOutputStream bos = new BufferedOutputStream(
new FileOutputStream("F:\\Test\\file4.zip"));
int data = 0;
int i =bis.available();
while ((data = bis.read()) !=-1) {
bos.write((byte)data);
}
bis.close();
bos.close();
}
}.getTime();
}
}
//抽象的不太好的模板设计模式
abstract class TimeTest {
void getTime() {
long start = System.currentTimeMillis();
try {
run();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println(System.currentTimeMillis() - start);
}
abstract void run() throws Exception;
}
测试数据248kb
测试结果:
有buff的File***Stream耗时:
8
有buff的Buffered***Stream耗时:
2
无buff的File***Stream耗时:
1369
无buff的Buffered***Stream耗时:
14
相关文章
- Java 并发工具包 java.util.concurrent 用户指南
- java虚拟机学习-JVM内存管理:深入Java内存区域与OOM(3)
- 你所不知道的库存超限做法 服务器一般达到多少qps比较好[转] JAVA格物致知基础篇:你所不知道的返回码 深入了解EntityFramework Core 2.1延迟加载(Lazy Loading) EntityFramework 6.x和EntityFramework Core关系映射中导航属性必须是public? 藏在正则表达式里的陷阱 两道面试题,带你解析Java类加载机制
- Java 按行读写文件(解决中文乱码)
- Java多线程系列—多线程带来的问题(05)
- 为什么开发人员从Java转到Go
- [转]java dd-m 月 - yy 日期转换成 yyyy-MM-dd
- JAVA单元测试框架-12-java代码重复执行失败的测试用例
- Ubuntu安装java的几种方式以及多个JAVA版本的切换
- java.lang.IllegalArgumentException: Invalid character found in method name
- java的锁机制
- 【Java】整理关于java的String类,equals函数和比较操作符的区别
- 【Java】+图表生成(8种图表工具)
- 为什么Java一直深受企业欢迎?选Java的优势在哪?
- 《Java EE 7精粹》—— 3.9 导航规则
- java 验证码图片处理类,为验证码识别做准备
- maven项目 Java compiler level does not match the version of the installed Java project facet
- java内部类详解
- JAVA学习.java.sql.date 与java.util.date以及gettime()方法的分析
- Java设计模式博客全文件夹
- Eclipse迅速执行:Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
- Java Swing编程接口(30)---列表框:JList
- 【Java】:Java当中为什么不能够直接用==比较String字符串