输入输出流实验_第1页
输入输出流实验_第2页
输入输出流实验_第3页
输入输出流实验_第4页
输入输出流实验_第5页
已阅读5页,还剩3页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

1、精选优质文档-倾情为你奉上深 圳 大 学实 验 报 告课程名称: Java 实验序号: 上机实践9 实验名称: 统计英文单词&读取Zip文件 班 级: 计算机3 姓 名: 卢志敏 同 组 人: 实验日期: 2008 年 12 月 29 日 教师签字: 一、实验目的n 掌握RandomAccessFile类的使用。n 掌握ZipInputStream流的使用。二、实验环境JDK1.5WinXp SP 33、 实验要求实验1n 使用RandomAccessFile流统计一篇英文中的单词,要求如下:(1) 一共出现了多少个英文单词。(2) 有多少个互不相同的单词。(3) 给出每个单词出现的频

2、率,并将这些单词按频率大小顺序显示在一个Text-Area中。实验2n 读取book.zip,并将book.zip中含有的文件重新存放到当前目录中的book文件夹中,即将book.zip的内容解压到book文件夹中。四、实验步骤和内容实验1源代码:WordStatistic.javaimport java.io.*;import java.util.Vector;public class WordStatisticVector allWords,noSameWord;WordStatistic()allWords=new Vector();noSameWord=new Vector();pub

3、lic void wordStatistic(File file)try RandomAccessFile inOne=new RandomAccessFile(file,"rw");/创建指向文件file的inOne的对象RandomAccessFile inTwo=new RandomAccessFile(file,"rw");/创建指向文件file的inTwo的对象long wordStarPostion=0,wordEndPostion=0;long length=inOne.length();int flag=1;int c=-1;for (i

4、nt k=0;k<=length;k+)c=inOne.read();/inOne调用read()方法boolean boo=(c<='Z'&&c>='A')|(c<='z'&&c>='a');if (boo)if (flag=1)wordStarPostion=inOne.getFilePointer()-1;flag=0;else if (flag=0)if (c=-1)wordEndPostion=inOne.getFilePointer();else wordE

5、ndPostion=inOne.getFilePointer()-1;inTwo.seek(wordStarPostion);/inTwo调用seek方法将读写位置移动到wordStarPostionBytecc=newbyte(int)wordEndPostion-(int)wordStarPostion;inTwo.readFully(cc);/inTwo调用readFully方法String word=new String(cc);allWords.add(word);if(!noSameWord.contains(word)noSameWord.add(word);flag=1;inO

6、ne.close();inTwo.close();catch(Exception e)public Vector getAllWords()return allWords;public Vector getNoSameWord()return noSameWord;StatisticFrame.javaimport java.awt.*;import java.awt.event.*;import java.util.Vector;import java.io.File;public class StatisticFrame extends Frame implements ActionLis

7、tenerWordStatistic statistic;TextArea showMessage;Button openFile;FileDialog openFileDialog;Vector allWord,noSameWord;public StatisticFrame()statistic=new WordStatistic();showMessage=new TextArea();openFile=new Button("Open File");openFile.addActionListener(this);add(openFile,BorderLayout.

8、NORTH);add(showMessage,BorderLayout.CENTER);openFileDialog=new FileDialog(this,"打开文件对话框",FileDialog.LOAD);allWord=new Vector();noSameWord=new Vector();setSize(350,300);setVisible(true);addWindowListener(new WindowAdapter()public void windowClosing(WindowEvent e)System.exit(0););validate();

9、public void actionPerformed(ActionEvent e)noSameWord.clear();allWord.clear();showMessage.setText(null);openFileDialog.setVisible(true);String fileName=openFileDialog.getFile();if(fileName!=null)statistic.wordStatistic(new File(fileName);allWord=statistic.getAllWords();noSameWord=statistic.getNoSameW

10、ord();showMessage.append("n"+fileName+"中有"+allWord.size()+"个英文单词");showMessage.append("n其中有"+noSameWord.size()+"个互不相同的英文单词");showMessage.append("n按使用频率排列:n");int count=new intnoSameWord.size();for(int i=0;i<noSameWord.size();i+)String s1

11、=(String)noSameWord.elementAt(i);for(int j=0;j<allWord.size();j+)String s2=(String)allWord.elementAt(j);if(s1.equals(s2)counti+;for(int m=0;m<noSameWord.size();m+)for(int n=m+1;n<noSameWord.size();n+)if(countn>countm)String temp=(String)noSameWord.elementAt(m);noSameWord.setElementAt(Str

12、ing)noSameWord.elementAt(n),m);noSameWord.setElementAt(temp,n);int t=countm;countm=countn;countn=t;for(int m=0;m<noSameWord.size();m+)showMessage.append("n"+(String)noSameWord.elementAt(m)+":"+countm+"/"+allWord.size()+"="+(1.0*countm)/allWord.size();Statis

13、ticMainClass.javapublic class StatisticMainClasspublic static void main(String args)new StatisticFrame();实验2源代码ReadZipFile.javaimport java.io.*;import java.util.zip.*;public class ReadZipFilepublic static void main(String args)File f = new File("book.zip");File dir = new File("Book&qu

14、ot;);byte b = new byte100;dir.mkdir();tryZipInputStream in = new ZipInputStream(new FileInputStream(f);ZipEntry zipEntry =null;while(zipEntry=in.getNextEntry()!=null)File file = new File(dir,zipEntry.getName();FileOutputStream out = new FileOutputStream(file);int n=-1;System.out.println(file.getAbso

15、lutePath()+"的内容");while(n=in.read(b,0,100)!=-1) String str= new String(b,0,n);System.out.println(str);out.write(b,0,n); out.close(); in.close();catch(IOException ee)System.out.println(ee); 运行效果截屏:5、 实验后的练习在StatisticFrame的showMessage中增加单词按字典序排序输出的信息。解答:在StatisticFrame.java中加入如下代码:Vector sor

16、tWord=new Vecor(noSameWord);showMessage.append("n按字典排序:n");for(int i=0;i<sortWord.size();i+)if(int)(String)sortWord.elementAt(i).charAt(0)>(int)(String)sortWord.elementAt(i+1).charAt(0)/比较相邻两个单词首字母在字母表中的大小顺序tempStr=(String)sortWord.elementAt(i);sortWord.elementAt(i)=sortWord.elementAt(i+1);sortWord.elementAt(i+1)=tempStr;/如果前面的大于后面的则对调for(int k=0;k<sortWord.size();k+)showMessage.append("n"+(String

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论