资源预览内容
第1页 / 共9页
第2页 / 共9页
第3页 / 共9页
第4页 / 共9页
第5页 / 共9页
第6页 / 共9页
第7页 / 共9页
第8页 / 共9页
第9页 / 共9页
亲,该文档总共9页全部预览完了,如果喜欢就下载吧!
资源描述
实验项目四 Java 高级编程第 1 部分 Java 输入/ 输出流 实验目的 1、掌握 java 常用输入、输出流类及其各自方法。2、能够灵活使用java 常用输入、输出流类及其各自方法解决实际应用问题。 实验要求 1、 复习理论教学中所学的内容。2、 认真进行实验预习,查阅参考书,书写源程序,书写实验预习报告。3、 认真总结实验并书写实验报告。 实验课时 2 学时 实验教学方式 学生上机实验,教师随堂指导。 实验内容 1 学读汉字1.模板代码/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(); 2 统计英文单词字/模板代码/WordStatistic.javaimport 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=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
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号