当前位置:文档之家› Java的输入与输出流(实验报告)

Java的输入与输出流(实验报告)

成都大学实验报告
实验项目名称Java的输入与输出流
一、实验目的:
1. 理解I/O流的概念,掌握其分类
2. 掌握文本文件读写、二进制文件读写
二、实验内容(包括源程序及相关说明):
1. 分别使用FileWriter 和 BufferedWriter 往文件中写入10万个随机数,比较用时。

源代码如下:
(1)
import .*;
public class Ex1_1 {
public static void main(String[] args) throws IOException{
long t=();
FileWriter fw =new FileWriter("d:\\");
for(int i=1;i<=100000;i++)
{
((int)()*10000)+" \n");
}
();
t=()-t;
"The elapsed: "+t);
}
}
(2)
import .*;
public class Ex1_1 {
public static void main(String[] args) throws IOException{
long t=();
BufferedWriter fw=new BufferedWriter(new FileWriter("d:\\"));
for(int i=1;i<=100000;i++){
((int)()*10000)+"\n");
}
();
t=()-t;
"The elapsed: "+t);
}
}
2. 生成一个 html文件,使其能显示2的幂次(0~9)的表格如下:
代码如下:
import .*;
public class Ex1_1 {
public static void main(String[] args) throws IOException{
BufferedWriter bw=new BufferedWriter(new FileWriter("d:\\"));
("<table border=1 align=center width=200 height=250>");
();
("<tr><td align=center>Power of 2<td align=center>Value</tr>");
for(int i=0;i<=9;i++){
("<tr><td align=center>"+i+"<td align=center>"+(i, 2)+"</tr>");
}
("</table>");
();
();
}
}
3. 在文本文件中包含有很长篇幅的英语短文,编写程序要求统计文件的所有短文中包含英文字母“A”的个数,并显示统计的时间。

第一种实现方法
代码如下:
import class EXP1_1 {
public static void main(String[] args) throws IOException{
long t =();
String filename="D:\\";
FileInputStream fis=new FileInputStream(filename);
int count=0;
int c;
while((c=())!=-1){
if(c=='A'){
count++;
}
}
();
t=()-t;
"Tim elapsed:"+t);
}
}
第二种方法
代码如下:
import class EXP1_1 {
public static void main(String[] args) throws IOException{
long t =();
String filename="D:\\";
FileInputStream fis=new FileInputStream(filename);
BufferedInputStream bis=new BufferedInputStream(fis);
int count=0;
int c;
while((c=())!=-1){
if(c=='A'){
count++;
}
}
();
t=()-t;
"Tim elapsed:"+t);
}
}
三、实验结果:
1、
2、
3、
4、
5、
四、实验小结与建议:
1、初步理解I/O流的概念,掌握其分类。

2、掌握文本文件读写、二进制文件读写。

成都大学信息科学与技术学院制。

相关主题