老师,请帮忙检查一下~

来源:3-16 自由编程

慕标5014141

2020-02-20 16:14:33

import java.io.BufferedOutputStream;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.util.Random;


public class Buffered {


public static void main(String[] args) {

try {

File file = new File("one.txt");

if (!file.exists()) {

file.createNewFile();

}

File file1 = new File("two.txt");

if (!file1.exists()) {

file1.createNewFile();

}

FileOutputStream fos = new FileOutputStream(file);

long start1 = System.currentTimeMillis();

for (int i = 1; i <= 10000; i++) {

Random rand = new Random();

int a = rand.nextInt(26) + 97;

fos.write((char) a);

}

long end1 = System.currentTimeMillis();

fos.close();

System.out.println(end1 - start1);

FileOutputStream fos1 = new FileOutputStream(file1);

BufferedOutputStream bos = new BufferedOutputStream(fos1);

long start2 = System.currentTimeMillis();

for (int i = 1; i <= 10000; i++) {

Random rand = new Random();

int a = rand.nextInt(26) + 97;

bos.write((char) a);

}

bos.flush();

bos.close();

long end2 = System.currentTimeMillis();

System.out.println(end2 - start2);

} catch (FileNotFoundException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}


}

}


写回答

1回答

好帮手慕小脸

2020-02-20

同学你好,代码编写的不错,继续加油!

如果我的回答解决了你的疑惑,请采纳!祝学习愉快!

0

0 学习 · 11489 问题

查看课程