一、实验目的:
1. 理解I/O流的概念,掌握其分类
2、掌握文本文件读写、二进制文件读写
二、实验环境:
JAVA + Window + Eclipse
三、实验内容:
1、分别使用FileWriter 和 BufferedWriter 往文件中写入1万个随机数,比较用时的多少?(用时采用方法System.currentTimeMillis())求时间差;
FileWriter:
package code5;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class filewrite {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stublong time2;
long time1=System.currentTimeMillis();
FileWriter f=new FileWriter("c.text");
int num;
Random random=new Random();
for(int i=0;i<=10000;i++){
f.write((int)random.nextDouble());
}
f.close();
time2=System.currentTimeMillis()-time1;
System.out.println("时间:"+time2+"微秒");
}
}
BufferedWriter:
package code5;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Random;
public class bufferedwriter {
public static void main(String[] args)throws IOException {
// TODO Auto-generated method stub
long time2;
long time1=System.currentTimeMillis();
BufferedWriter f1=new BufferedWriter(new FileWriter("c.text1"));
Random random=new Random();
for(int i=0;i<=10000;i++)
{
f1.write((int)random.nextDouble());
}
f1.close();
time2