交作业交作业
来源:3-16 自由编程
mixiaofan
2019-09-18 16:26:56
package com.imooc.lianxiyi; import java.io.BufferedOutputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; public class Test { public static void main(String[] args) { // TODO Auto-generated method stub try { System.out.println("one.txt不用缓冲流来写"); FileOutputStream fos = new FileOutputStream("one.txt"); long startTimeOne = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { fos.write(1); } fos.close(); long endTimeOne = System.currentTimeMillis(); System.out.println("one用时" + (endTimeOne - startTimeOne)); System.out.println("two.txt用缓冲流来写"); FileOutputStream tfos = new FileOutputStream("two.txt"); BufferedOutputStream tbos = new BufferedOutputStream(tfos); long startTime = System.currentTimeMillis(); for (int i = 0; i < 100000; i++) { tbos.write(1); } tbos.flush(); tbos.close(); long endTime = System.currentTimeMillis(); System.out.println("two用时" + (endTime - startTime)); System.out.println("节约时间为:" + ((endTimeOne - startTimeOne) - (endTime - startTime))); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }
1回答
好帮手慕酷酷
2019-09-18
同学你好,代码完成的不错,但是建议,在关闭流时,应该将所有的流都关闭,养成良好的编码习惯~具体如下:
如果我的回答解决了你的疑惑,请采纳!祝学习愉快!