请老师检查 谢谢

来源:3-16 自由编程

JovetWang

2022-05-09 22:41:40

package com.imooc.Test2;

import java.io.*;

public class Test {
    public static void main(String[] args) throws IOException {
        File one = new File("d://one.txt");
        File two = new File("d://two.txt");
        FileOutputStream fos = new FileOutputStream(one);
        FileOutputStream f = new FileOutputStream(two);
        BufferedOutputStream bos = new BufferedOutputStream(f);

        System.out.println(one.getName() + "不使用缓冲流写入。");
        long startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            fos.write(1);
        }
        long endTime = System.currentTimeMillis();
        long oneTime = endTime - startTime;
        System.out.println("耗时:" + oneTime);

        System.out.println(one.getName() + "使用缓冲流写入。");
        startTime = System.currentTimeMillis();
        for (int i = 0; i < 10000; i++) {
            bos.write(1);
        }
        endTime = System.currentTimeMillis();
        long twoTime = endTime - startTime;
        System.out.println("耗时:" + twoTime);

        System.out.println("节省时间:" + (oneTime - twoTime));
        bos.close();
        fos.close();
        f.close();
    }
}


控制台运行结果:

one.txt不使用缓冲流写入。
耗时:146
one.txt使用缓冲流写入。
耗时:1
节省时间:145


写回答

1回答

好帮手慕小蓝

2022-05-10

同学你好,同学的代码符合题目要求,逻辑清晰,书写规范,做的很棒。

祝学习愉快~


0

0 学习 · 16556 问题

查看课程