Java实验9 T1.往文件中写入1万个随机数,比较用时的多少
程序员文章站
2022-07-10 08:17:02
题目要求分别使用FileWriter 和 BufferedWriter 往文件中写入1万个随机数,比较用时的多少?(用时采用方法System.currentTimeMillis())求时间差;FileWriterimport java.io.FileOutputStream;import java.io.IOException;public class Test1 {public static void main(String[] args) throws IOException {F...
题目要求
分别使用FileWriter 和 BufferedWriter 往文件中写入1万个随机数,比较用时的多少?(用时采用方法System.currentTimeMillis())求时间差;
FileWriter
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
FileOutputStream output = new FileOutputStream("temp.dat");
long start = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
output.write((int) (Math.random()*10));
}
long stop = System.currentTimeMillis();
long time = stop-start;
System.out.println("时间差为:"+ time +"毫秒");
}
}
BufferedWriter
import java.io.BufferedOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
public class Test1 {
public static void main(String[] args) throws IOException {
BufferedOutputStream bouput = new BufferedOutputStream(
new FileOutputStream("temp2.dat"));
long start2 = System.currentTimeMillis();
for (int i = 0; i < 10000; i++) {
bouput.write((int) (Math.random()*10));
}
long stop2 = System.currentTimeMillis();
long time2 = stop2-start2;
System.out.println("BufferedWriter的时间差为:"+ time2 +" 毫秒");
}
}
本文地址:https://blog.csdn.net/weixin_45884316/article/details/111943317