Java的输入与输出流(实验报告)
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
成都大学实验报告
实验项目名称Java的输入与输出流
一、实验目的:
1、理解I/O流的概念,掌握其分类
2、掌握文本文件读写、二进制文件读写
二、实验内容(包括源程序及相关说明):
1、分别使用与BufferedWriter 往文件中写入10万个随机数,比较用时。源代码如下:
(1)
import java、io、*;
public class Ex1_1 {
public static void main(String[] args) throws IOException{ long t=System、currentTimeMillis();
fw =new ("d:\\Ex1、txt");
for(int i=1;i<=100000;i++)
{
fw、write((int)(Math、random()*10000)+" \n");
}
fw、close();
t=System、currentTimeMillis()-t;
System、out、println("The elapsed: "+t);
}
}
(2)
import java、io、*;
public class Ex1_1 {
public static void main(String[] args) throws IOException{ long t=System、currentTimeMillis();
BufferedWriter fw=new BufferedWriter(new ("d:\\Ex1、txt"));
for(int i=1;i<=100000;i++){
fw、write((int)(Math、random()*10000)+"\n");
}
fw、close();
t=System、currentTimeMillis()-t;
System、out、println("The elapsed: "+t);
}
}
2、生成一个html文件,使其能显示2的幂次(0~9)的表格如下:
代码如下:
import java、io、*;
public class Ex1_1 {
public static void main(String[] args) throws IOException{ BufferedWriter bw=new BufferedWriter(new ("d:\\Ex2、html"));
bw、write("
Power of 2 | align=center>Value |
"+i+" | "+Math、pow(i, 2)+" |
bw、newLine();
bw、close();
}
}
3、在文本文件bigbook、txt中包含有很长篇幅的英语短文,编写程序要求统计文件的所有短文中包含英文字母“A”的个数,并显示统计的时间。
第一种实现方法
代码如下:
import java、io、;
import java、io、IOException;
public class EXP1_1 {
public static void main(String[] args) throws IOException{ long t =System、currentTimeMillis();
String "D:\\bigbook、txt";
fis=new ();
int count=0;
int c;
while((c=fis、read())!=-1){
if(c=='A'){
count++;
}
}
fis、close();
System、out、println(count);
t=System、currentTimeMillis()-t;
System、out、println("Tim elapsed:"+t);
}
}
第二种方法
代码如下:
import java、io、;
import java、io、IOException;
import java、io、BufferedInputStream;
public class EXP1_1 {
public static void main(String[] args) throws IOException{ long t =System、currentTimeMillis();
String "D:\\bigbook、txt";
fis=new ();
BufferedInputStream bis=new BufferedInputStream(fis);
int count=0;
int c;
while((c=bis、read())!=-1){
if(c=='A'){
count++;
}
}
fis、close();
System、out、println(count);
t=System、currentTimeMillis()-t;
System、out、println("Tim elapsed:"+t);
}
}
三、实验结果:
1、