老师麻烦看看有没有问题,这时间差得太多了哈哈

来源:3-16 自由编程

强壮的蜗牛

2019-08-22 21:32:50


package lianxi;


import java.io.BufferedOutputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;


public class Demo3 {


public static void main(String[] args) {

FileOutputStream fos;

long a=0;

try {

System.out.println("one.txt不使用缓冲流来写");

fos=new FileOutputStream("one.txt");

long startTime1=System.currentTimeMillis();

for(int i=1;i<=100000;i++) {

fos.write('a');

}

long endTime1=System.currentTimeMillis();

a=(endTime1-startTime1);

System.out.println("用时为:"+a);

fos.close();

} catch (FileNotFoundException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

} catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

FileOutputStream fos1;

BufferedOutputStream bos;

long b=0;

try {

System.out.println("two.txt使用缓冲流来写");

fos1=new FileOutputStream("two.txt");

bos=new BufferedOutputStream(fos1);

long startTime2=System.currentTimeMillis();

for(int i=1;i<=100000;i++) {

bos.write('a');

}

bos.flush();

long endTime2=System.currentTimeMillis();

b=endTime2-startTime2;

System.out.println("用时为:"+b);

fos1.close();

bos.close();

} catch (FileNotFoundException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

} catch (IOException e) {

// TODO 自动生成的 catch 块

e.printStackTrace();

}

System.out.println("节省时间"+(a-b)+"ms");

}

}


http://img.mukewang.com/climg/5d5e995c0001b0fd02970228.jpg

写回答

1回答

好帮手慕珊

2019-08-23

同学,你好!代码是没有问题的,确实时间差很多。

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 11489 问题

查看课程