JAVA上机实验8
实验名称: 输入输出流与文件处理
实验目的:掌握字符输入、输出流的用法;掌握RandomAccessFile类的使用;掌握ZipInputStream流的使用。
实验要求:
一、编写一个Java应用程序,要求如下:
1、
可以将一个由汉字字符组成的文本文件读入到程序中。
2、
单击名为“下一个汉字”的按钮,可以在一个标签中显示程序读入的一个汉字。
3、
单击名为“发音”的按钮,可以听到标签上显示的汉字的拼音。
4、
用户可以使用文本编辑器编辑程序中用到的3个由汉字字符组成的文本文件:training1.txt、training2.txt和training3.txt,这些文本文件中的汉字需要用空格、逗号或回车分隔。
5、
需要自己制作相应的声音文件,比如,training1.txt包含汉字“你”,那么在当前应用程序的运行目录中血药有“你.wav”格式的声音文件。
6、
用户选择“帮助”菜单,可以查看软件的帮助信息。
程序模版
请按照模版要求,将【代码】替换为程序代码。
ChineseCharacters.java
import java.io.*;
import java.util.StringTokenizer;
public class ChineseCharacters
{ public StringBuffer
getChinesecharacters(File file)
{
StringBuffer hanzi=new StringBuffer();
try{
FileReader
inOne=【代码1】
//创建指向文件f的inOne
的对象
BufferedReader inTwo=【代码2】 //创建指向文件inOne的inTwo 的对象
String
s=null;
int i=0;
while((s=【代码3】)!=null)
//inTwo读取一行
{
StringTokenizer tokenizer=new StringTokenizer(s," ,'\n'
");
while(tokenizer.hasMoreTokens())
{
hanzi.append(tokenizer.nextToken());
}
}
}
catch(Exception e) {}
return hanzi;
}
}
StudyFrame.java
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
public class StudyFrame extends Frame implements
ItemListener,ActionListener,Runnable
{ ChineseCharacters
chinese;
Choice choice;
Button getCharacters,voiceCharacters;
Label showCharacters;
StringBuffer trainedChinese=null;
Clip clip=null;
Thread voiceThread;
int
k=0;
Panel pCenter;
CardLayout mycard;
TextArea textHelp;
MenuBar menubar;
Menu menu;
MenuItem help;
public StudyFrame()
{
chinese=new ChineseCharacters();
choice=new Choice();
choice.add("training1.txt");
choice.add("training2.txt");
choice.add("training3.txt");
showCharacters=new Label("",Label.CENTER);
showCharacters.setFont(new Font("宋体",Font.BOLD,72));
showCharacters.setBackground(Color.green);
getCharacters=new Button("下一个汉字");
voiceCharacters=new Button("发音");
voiceThread=new Thread(this);
choice.addItemListener(this);
voiceCharacters.addActionListener(this);
getCharacters.addActionListener(this);
Panel pNorth=new Panel();
pNorth.add(new Label("选择一个汉字字符组成的文件"));
pNorth.add(choice);
add(pNorth,BorderLayout.NORTH);
Panel pSouth=new Panel();
pSouth.add(getCharacters);
pSouth.add(voiceCharacters);
add(pSouth,BorderLayout.SOUTH);
pCenter=new Panel();
mycard=new CardLayout();
pCenter.setLayout(mycard);
textHelp=new TextArea();
pCenter.add("hanzi",showCharacters);
pCenter.add("help",textHelp);
add(pCenter,BorderLayout.CENTER);
menubar=new MenuBar();
menu=new Menu("帮助");
help=new MenuItem("关于学汉字");
help.addActionListener(this);
menu.add(help);
menubar.add(menu);
setMenuBar(menubar);
setSize(350,220);
setVisible(true);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
validate();
}
public void
itemStateChanged(ItemEvent e)
{ String
fileName=choice.getSelectedItem();
File file=new File(fileName);
trainedChinese=chinese.getChinesecharacters(file);
k=0;
mycard.show(pCenter,"hanzi") ;
}
public void
actionPerformed(ActionEvent e)
{
if(e.getSource()==getCharacters)
{
if(trainedChinese!=null)
{ char
c=trainedChinese.charAt(k);
k++;
if(k>=trainedChinese.length())
k=0;
showCharacters.setText(""+c);
}
else
{ showCharacters.setText("请选择一个汉字字符文件");
}
}
if(e.getSource()==voiceCharacters)
{
if(!(voiceThread.isAlive()))
{ voiceThread=new Thread(this);
}
try{ voiceThread.start();
}
catch(Exception exp){}
}
if(e.getSource()==help)
{
mycard.show(pCenter,"help") ;
try{ File helpFile=new File("help.txt");
FileReader
inOne=【代码4】
//创建指向文件helpFile的inOne 的对象
BufferedReader
inTwo=【代码5】 //创建指向文件inOne的inTwo 的对象
String
s=null;
while((s=inTwo.readLine())!=null)
{
textHelp.append(s+"\n");
}
inOne.close();
inTwo.close();
}
catch(IOException exp){}
}
}
public void
run()
{
voiceCharacters.setEnabled(false);
try{ if(clip!=null)
{ clip.close()
}
clip=AudioSystem.getClip();
File voiceFile=new
File(showCharacters.getText().trim()+".wav");
clip.open(AudioSystem.getAudioInputStream(voiceFile));
}
catch(Exception exp){}
clip.start();
voiceCharacters.setEnabled(true);
}
}
StudyMainClass.java
public class StudyMainClass
{ public static void
main(String args[])
{ new
StudyFrame();
}
}
二、使用RandomAccessFile流统计一篇英文中的单词,要求如下:
1、
一共出现了多少个单词
2、
有多少个互不相同的单词
3、
给出每个单词出现的频率,并将这些单词按频率大小顺序显示在一个TextArea中。
程序模版
请按照模版要求,将【代码】替换为程序代码。
WordStatistic.java
import java.io.*;
import java.util.Vector;
public class WordStatistic
{ Vector allWorsd,noSameWord;
WordStatistic()
{ allWorsd=new
Vector();
noSameWord=new Vector();
}
public void
wordStatistic(File file)
{ try{
RandomAccessFile
inOne=【代码1】
//创建指向文件file的inOne 的对象
RandomAccessFile inTwo=【代码2】
//创建指向文件file的inTwo 的对象
long wordStarPostion=0,wordEndPostion=0;
long length=inOne.length();
int flag=1;
int c=-1;
for(int
k=0;k<=length;k++)
{
c=【代码3】
// 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
wordEndPostion=inOne.getFilePointer()-1;
【代码4】//
inTwo调用seek方法将读写位置移动到wordStarPostion
byte cc[]=new
byte[(int)wordEndPostion-(int)wordStarPostion];
【代码5】//
inTwo调用readFully(byte
a)方法,向a传递cc
String word=new String(cc);
allWorsd.add(word);
if(!(noSameWord.contains(word)))
noSameWord.add(word);
}
flag=1;
}
}
inOne.close();
inTwo.close();
}
catch(Exception e){}
}
public Vector
getAllWorsd()
{ return
allWorsd;
}
public Vector
getNoSameWord()
{ return
noSameWord;
}
}
RandomExample.java
import java.awt.*;
import java.awt.event.*;
import java.util.Vector;
import java.io.File;
public 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");
openFile.addActionListener(this);
add(openFile,BorderLayout.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();
}
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.getAllWorsd();
noSameWord=statistic.getNoSameWord();
showMessage.append("\n"+fileName+"中有"+allWord.size()+"个英文单词");
showMessage.append("\n其中有"+noSameWord.size()+"个互不相同英文单词");
showMessage.append("\n按使用频率排列:\n");
int count[]=new int[noSameWord.size()];
for(int i=0;i<noSameWord.size();i++)
{ String
s1=(String)noSameWord.elementAt(i);
for(int j=0;j<allWord.size();j++)
{
String s2=(String)allWord.elementAt(j);
if(s1.equals(s2))
count[i]++;
}
}
for(int m=0;m<noSameWord.size();m++)
{ for(int
n=m+1;n<noSameWord.size();n++)
{ if(count[n]>count[m])
{ String
temp=(String)noSameWord.elementAt(m);
noSameWord.setElementAt((String)noSameWord.elementAt(n),m);
noSameWord.setElementAt(temp,n);
int t=count[m];
count[m]=count[n];
count[n]=t;
}
}
}
for(int m=0;m<noSameWord.size();m++)
{
showMessage.append("\n"+(String)noSameWord.elementAt(m)+
":"+count[m]+"/"+allWord.size()+
"="+(1.0*count[m])/allWord.size());
}
}
}
}
RandomExample.java
public class StatisticMainClass
{ public static void
main(String args[])
{ new
StatisticFrame();
}
}
加载中,请稍候......