《java上机2参考代码》由会员分享,可在线阅读,更多相关《java上机2参考代码(9页珍藏版)》请在金锄头文库上搜索。
1、文档供参考,可复制、编制,期待您的好评与关注! 上机实践2 输入输出流实验1 学读汉字1答案:【代码1】: new FileReader(file);【代码2】: new BufferedReader(inOne); 【代码3】: inTwo.readLine(); 【代码4】: new FileReader(helpFile); 【代码5】: new BufferedReader(inOne); 2模板代码 ChineseCharacters.javaimport java.io.*;import java.util.StringTokenizer;public class ChineseC
2、haracters public StringBuffer getChinesecharacters(File file) StringBuffer hanzi=new StringBuffer(); try FileReader inOne= new FileReader(file); /创建指向文件f的inOne 的对象 BufferedReader inTwo= new BufferedReader(inOne); /创建指向文件inOne的inTwo 的对象 String s=null; int i=0; while(s= inTwo.readLine()!=null) /inTwo读
3、取一行 StringTokenizer tokenizer=new StringTokenizer(s, ,n ); while(tokenizer.hasMoreTokens() hanzi.append(tokenizer.nextToken(); catch(Exception e) return hanzi; StudyFrame.javaimport java.awt.*;import java.awt.event.*;import java.io.*;import javax.sound.sampled.*;public class StudyFrame extends Frame
4、 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;
5、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
6、(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
7、); 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,t
8、extHelp); 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(Wi
9、ndowEvent 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
10、()=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();
11、catch(Exception exp) if(e.getSource()=help) mycard.show(pCenter,help) ; try File helpFile=new File(help.txt); FileReader inOne= new FileReader(helpFile); /创建指向文件helpFile的inOne 的对象 BufferedReader inTwo= new BufferedReader(inOne); /创建指向文件inOne的inTwo 的对象 String s=null; while(s=inTwo.readLine()!=null) t
12、extHelp.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(