我是不是哪里写错了。。我的用时好长

我是不是哪里写错了。。我的用时好长

package FileOutputStream;


import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.OutputStream;


public class text {


public static void main(String[] args) {

// TODO Auto-generated method stub

File file = new File("D:\\File\\one.txt");

if (file.exists()) {

try {

file.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


File file1 = new File("D:\\File\\two.txt");

if (file.exists()) {

try {

file1.createNewFile();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}


long startTime;

long endTime;

long time1 = 0;

long time2 = 0;


try {

FileOutputStream one = new FileOutputStream(file);

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

startTime = System.currentTimeMillis();

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

one.write((int) i);

}

one.close();

endTime = System.currentTimeMillis();

time1 = endTime - startTime;

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

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


try {

BufferedOutputStream two = new BufferedOutputStream(new FileOutputStream(file1));

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

startTime = System.currentTimeMillis();

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

two.write((int) i);

}

two.flush();

two.close();

endTime = System.currentTimeMillis();

time2 = endTime - startTime;

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


} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

System.out.println("节省时间:" + (time1 - time2) + "ms");


}


}

http://img1.sycdn.imooc.com//climg/5ea8ce8e091b205f02500152.jpg

http://img1.sycdn.imooc.com//climg/5ea8ce9d0917b8ff02510141.jpg

正在回答

登陆购买课程后可参与讨论,去登陆

1回答

同学你好,同学的代码写的没错,用时长短是跟电脑有关系的,而且每次时间是不固定的,这个练习题是为了让大家体验一下使用缓冲流比不使用快,老师用的时间更长

http://img1.sycdn.imooc.com//climg/5ea8f1b409a467ce03960197.jpg

祝学习愉快

  • 慕粉1002055491 提问者 #1
    让公司给你们换台好点的电脑!
    2020-04-29 11:26:52
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师