老师帮忙检查一下!

来源:3-16 自由编程

全栈丶obj

2020-01-29 00:41:30

import java.io.*;

public class docWrite {
    public static void main(String[] args) {
        long time = 0;
        long Time;
        try {
            FileOutputStream f1 = new FileOutputStream("D:\\photo\\one.txt");
            long startTime = System.currentTimeMillis();
            System.out.println("one.txt不使用缓冲来写");
            for (int i = 0; i <1000000 ; i++) {
                f1.write((int)i);
            }
            f1.close();
            long endTime = System.currentTimeMillis();
            time = endTime-startTime;
            System.out.println("用时为:"+time);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            BufferedOutputStream f2 = new BufferedOutputStream(new FileOutputStream("D:\\photo\\two.txt"));
            long starTime = System.currentTimeMillis();
            for (int i = 0; i <1000000 ; i++) {
                f2.write((char)i);
            }
            f2.flush();
            f2.close();
            long endTime = System.currentTimeMillis();
            Time = endTime-starTime;
            System.out.println("使用缓冲的时间为"+Time);
            System.out.println("节省时间"+(time-Time)+"ms");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}


写回答

1回答

好帮手慕柯南

2020-01-31

同学完成的不错,加油,祝学习愉快~

0

0 学习 · 11489 问题

查看课程