课题打卡~请老师检查

来源:3-16 自由编程

慕仙4530950

2020-06-18 18:29:22

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回答

好帮手慕阿慧

2020-06-18

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

0

0 学习 · 16556 问题

查看课程