实验项目四第1部分

上传人:飞*** 文档编号:53325344 上传时间:2018-08-29 格式:PDF 页数:9 大小:10.29KB
返回 下载 相关 举报
实验项目四第1部分_第1页
第1页 / 共9页
实验项目四第1部分_第2页
第2页 / 共9页
实验项目四第1部分_第3页
第3页 / 共9页
实验项目四第1部分_第4页
第4页 / 共9页
实验项目四第1部分_第5页
第5页 / 共9页
点击查看更多>>
资源描述

《实验项目四第1部分》由会员分享,可在线阅读,更多相关《实验项目四第1部分(9页珍藏版)》请在金锄头文库上搜索。

1、实验项目四 Java 高级编程第 1 部分 Java 输入/ 输出流 实验目的 1、掌握 java 常用输入、输出流类及其各自方法。2、能够灵活使用java 常用输入、输出流类及其各自方法解决实际应用问题。 实验要求 1、 复习理论教学中所学的内容。2、 认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。3、 认真总结实验并书写实验报告。 实验课时 2 学时 实验教学方式 学生上机实验,教师随堂指导。 实验内容 1 学读汉字1.模板代码/ChineseCharacters.java import java.io.*; import java.util.StringTokenizer;

2、 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 to

3、kenizer=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 It

4、emListener,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; Men

5、uItem 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(Col

6、or.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(ch

7、oice); 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(

8、“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 wi

9、ndowClosing(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

10、) 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 vo

11、iceThread.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+

12、“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(Exc

13、eption exp) clip.start(); voiceCharacters.setEnabled(true); /StudyMainClass.java public class StudyMainClass public static void main(String args) new StudyFrame(); 2 统计英文单词字/模板代码/WordStatistic.javaimport java.io.*; import java.util.Vector; public class WordStatistic Vector allWorsd,noSameWord; WordS

14、tatistic() 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(i

15、nt k=0;k=A)|(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 传递 ccString word=new String(cc); allWorsd.add(word); if(!(noSameWord.contains(word) noSameWord.add(word); flag=1; inOne.close(); inTwo.close(); catch(Exception e) public

展开阅读全文
相关资源
正为您匹配相似的精品文档
相关搜索

最新文档


当前位置:首页 > 行业资料 > 其它行业文档

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号