輸入輸出流實驗_第1頁
輸入輸出流實驗_第2頁
輸入輸出流實驗_第3頁
輸入輸出流實驗_第4頁
輸入輸出流實驗_第5頁
已閱讀5頁,還剩3頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領

文檔簡介

1、精選優(yōu)質文檔-傾情為你奉上深 圳 大 學實 驗 報 告課程名稱: Java 實驗序號: 上機實踐9 實驗名稱: 統(tǒng)計英文單詞&讀取Zip文件 班 級: 計算機3 姓 名: 盧志敏 同 組 人: 實驗日期: 2008 年 12 月 29 日 教師簽字: 一、實驗目的n 掌握RandomAccessFile類的使用。n 掌握ZipInputStream流的使用。二、實驗環(huán)境JDK1.5WinXp SP 33、 實驗要求實驗1n 使用RandomAccessFile流統(tǒng)計一篇英文中的單詞,要求如下:(1) 一共出現(xiàn)了多少個英文單詞。(2) 有多少個互不相同的單詞。(3) 給出每個單詞出現(xiàn)的頻

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");/創(chuàng)建指向文件file的inOne的對象RandomAccessFile inTwo=new RandomAccessFile(file,"rw");/創(chuàng)建指向文件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. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
  • 4. 未經權益所有人同意不得將文件中的內容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
  • 6. 下載文件中如有侵權或不適當內容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論