课题打卡~请老师检查

课题打卡~请老师检查

public class FileOutputStreamDemo1 {

	public static void main(String[] args) {
		// 创建文件
		File file = new File("/Users/mac/Desktop/one.txt");
		File file1 = new File("/Users/mac/Desktop/two.txt");
		try {
			file.createNewFile();
			file1.createNewFile();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		FileOutputStream fos;	
		FileOutputStream fos1;	
		try {
			fos = new FileOutputStream(file);
			long startTime = System.currentTimeMillis();
			for (int i = 0; i < 100000; i++) {
				fos.write('a');				
			}
			System.out.println(file.getName() + "不使用缓冲流来写");
            long endTime = System.currentTimeMillis();
            System.out.println("用时为:"+(endTime-startTime));
            fos1 = new FileOutputStream(file1);
            BufferedOutputStream bos = new BufferedOutputStream(fos1);
            long startTime1 = System.currentTimeMillis();
            for (int i = 0; i < 100000; i++) {
				bos.write('a');
			}
			bos.flush();
            long endTime1 = System.currentTimeMillis();
            System.out.println(file1.getName() + "使用缓冲流来写");
            System.out.println("用时为:"+(endTime1-startTime1));
            System.out.println("节省时间:"+((endTime-startTime)-(endTime1-startTime1))+"ms");
			fos.close();
			fos1.close();
			bos.close();
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}


正在回答

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

1回答

同学你好,代码没有问题,完成的很棒!继续加油~如果我的回答解决了你的疑惑,请采纳!祝学习愉快~

问题已解决,确定采纳
还有疑问,暂不采纳

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

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

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

在线咨询

领取优惠

免费试听

领取大纲

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