当前位置:文档之家› 输入输出流实验

输入输出流实验

深圳大学
实验报告
课程名称: Java
实验序号:上机实践9 实验名称:统计英文单词&读取Zip文件
班级:计算机3 姓名:卢志敏
同组人:实验日期: 2008 年 12 月 29 日
教师签字:
一、实验目的
掌握RandomAccessFile类的使用。

掌握ZipInputStream流的使用。

二、实验环境
WinXp SP 3
三、实验要求
实验1
使用RandomAccessFile流统计一篇英文中的单词,要求如下:
(1)一共出现了多少个英文单词。

(2)有多少个互不相同的单词。

(3)给出每个单词出现的频率,并将这些单词按频率大小顺序显示在一个Text--Area中。

实验2
读取,并将中含有的文件重新存放到当前目录中的book文件夹中,即将的内容解压到book文件夹中。

四、实验步骤和内容
实验1
源代码:
import .*;
import class WordStatistic
{ Vector allWords,noSameWord;
WordStatistic()
{ allWords=new Vector();
noSameWord=new Vector();
}
public void wordStatistic(File file)
{try { RandomAccessFile inOne=new RandomAccessFile(file,"rw");;
import class StatisticFrame extends Frame implements ActionListener
{ WordStatistic statistic;
TextArea showMessage;
Button openFile;
FileDialog openFileDialog;
Vector allWord,noSameWord;
public StatisticFrame()
{ statistic=new WordStatistic();
showMessage=new TextArea();
openFile=new Button("Open File");
(this);
add(openFile,;
add(showMessage,;
openFileDialog=new FileDialog(this,"打开文件对话框",;
allWord=new Vector();
noSameWord=new Vector();
setSize(350,300);
setVisible(true);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ (0);
}
});
validate();
}
public void actionPerformed(ActionEvent e)
{ ();
();
(null);
(true);
String fileName=();
if(fileName!=null)
{ (new File(fileName));
allWord=();
noSameWord=();
("\n"+fileName+"中有"+()+"个英文单词"); ("\n其中有"+()+"个互不相同的英文单词"); ("\n按使用频率排列: \n");
int count[]=new int[()];
for(int i=0;i<();i++)
{ String s1=(String)(i);
for(int j=0;j<();j++)
{ String s2=(String)(j);
if(s2))
count[i]++;
}
}
for(int m=0;m<();m++)
{ for(int n=m+1;n<();n++)
{ if(count[n]>count[m])
{ String temp=(String)(m);
((String)(n),m);
(temp,n);
int t=count[m];
count[m]=count[n];
count[n]=t;
}
}
}
for(int m=0;m<();m++)
{
("\n"+(String)(m)+":"+count[m]+"/"+()+"="+*count[m])/());
}
}
}
}
public class StatisticMainClass
{ public static void main(String args[])
{ new StatisticFrame();
}
}
实验2
源代码
import .*;
import class ReadZipFile
{
public static void main(String [] args)
{ File f = new File("");
File dir = new File("Book");
byte b[] = new byte[100];
();
try{ ZipInputStream in = new ZipInputStream(new FileInputStream(f));
ZipEntry zipEntry =null;
while((zipEntry=())!=null)
{
File file = new File(dir,());
FileOutputStream out = new FileOutputStream(file);
int n=-1;
"的内容");
while((n=(b,0,100))!=-1)
{ String str= new String(b,0,n);
(b,0,n);
}
();
}
();
}
catch(IOException ee)
{
}
}
}
运行效果截屏:
五、实验后的练习
在StatisticFrame的showMessage中增加单词按字典序排序输出的信息。

解答:
在中加入如下代码:
Vector sortWord=new Vecor(noSameWord);
("\n按字典排序: \n");
for(int i=0;i<();i++)
{
if((int)(String)(i).charAt(0)>(int)(String)(i+1).charAt(0)) //比较相邻两个单词首字母在字母表中的大小顺序
{ tempStr=(String)(i);
(i)=(i+1);
(i+1)=tempStr;
} //如果前面的大于后面的则对调
}
for(int k=0;k<();k++)
("\n"+(String)(k));
六、心得体会
1.RandomAccessFile类中的seek(long a)方法用来移动流的读写位置,getFilePointer()方法可以获取当前流的读写位置。

2.Vector()对象可以使用add(Object o)方法把对象自动添加到向量末尾。

3. ZipInputStream流必须指向一个字节流的子类的实例。

4.使用ZipInputStream流之前要先使用getNextEntry()方法找到压缩文件中的下一个内容,然后开始读取。

相关主题