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

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

成都大学实验报告
实验项目名称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("<table border=1 align=center width=200 height=250>");
bw、newLine();
bw、write("<tr><td align=center>Power of 2<td
align=center>Value</tr>");
for(int i=0;i<=9;i++){
bw、write("<tr><td align=center>"+i+"<td align=center>"+Math、pow(i, 2)+"</tr>");
}
bw、write("</table>");
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、
2、
3、
4、
5、
四、实验小结与建议:
1、初步理解I/O流的概念,掌握其分类。

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

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

相关主题