为什么下面代码的运行结果是使用缓冲流写数据比不使用缓冲流写数据还要慢?
package com.imooc;
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileDemo3 {
public static void main(String[] args) {
byte[] bytes = new byte[100000000];
for (int i = 0; i < bytes.length;i++) {
bytes[i] = 'a';
}
try {
FileOutputStream fos = new FileOutputStream("one.txt");
FileOutputStream fos1 = new FileOutputStream("two.txt");
BufferedOutputStream bos = new BufferedOutputStream(fos1);
long startTime = System.currentTimeMillis();
fos.write(bytes);
long endTime = System.currentTimeMillis();
System.out.println("one.txt不使用缓冲流来写用时为:" + (endTime - startTime));
long startTime1 = System.currentTimeMillis();
bos.write(bytes);
bos.flush();
long endTime1 = System.currentTimeMillis();
System.out.println("two.txt使用缓冲流来写用时为:" + (endTime1 - startTime1));
fos.close();
fos1.close();
bos.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}
}
3
收起
正在回答 回答被采纳积分+1
3回答
weixin_慕勒8187086
2019-08-22 22:11:57
package io;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class TestBuffer {
public static void main(String[] args) {
// TODO Auto-generated method stub
String f1 = "one.txt";
String f2 = "two.txt";
String f3 = "three.txt";
String f4 = "four.txt";
byte[] x = new byte[100000];
for(int i = 0; i<x.length; i++) {
x[i] = 'a';
}
FileOutputStream fos1, fos2a;
BufferedOutputStream bos1, bos2a;
long t1, t2;
// fos time
try {
fos1 = new FileOutputStream(f1);
t1 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
fos1.write('a');
}
fos1.close();
t2 = System.currentTimeMillis();
System.out.println("fos time: " + (t2 - t1));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// fos with array time
try {
fos2a = new FileOutputStream(f2);
t1 = System.currentTimeMillis();
fos2a.write(x,0,x.length);
fos2a.close();
t2 = System.currentTimeMillis();
System.out.println("fos with array time: " + (t2 - t1));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//bos time
FileOutputStream fos3;
try {
fos3 = new FileOutputStream(f3);
bos1 = new BufferedOutputStream(fos3);
t1 = System.currentTimeMillis();
for (int i = 0; i < 100000; i++) {
bos1.write('a');
}
bos1.flush();
fos3.close();
bos1.close();
t2 = System.currentTimeMillis();
System.out.println("bos time: " + (t2 - t1));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// bos with arraty time
FileOutputStream fos4;
try {
fos4 = new FileOutputStream(f4);
bos2a = new BufferedOutputStream(fos4);
t1 = System.currentTimeMillis();
bos2a.write(x, 0, x.length);
bos2a.flush();
fos4.close();
bos2a.close();
t2 = System.currentTimeMillis();
System.out.println("bos with array time: " + (t2 - t1));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}不论用不用buffer还是write(int b) 都要比write(byte[] b, int off, int len)慢啊?还是我的测试程序写的有问题?
1. Java 零基础入门
- 参与学习 人
- 提交作业 3802 份
- 解答问题 11489 个
本阶段带你迈入Java世界,学习Java必备基础知识,基础语法、面向对象思想以及常用工具类的使用。
了解课程




恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星