当前位置:文档之家› java 输入输出流实验

java 输入输出流实验

EnglishWord english;
JComboBox choice;
JButton getWord,voiceWord;
JLabel showWord;
String trainedWord=null;
Clip clip=null;
Tread voiceThread;
int k=0;
JPanel pCenter;
public StringBuffer getEnglishWord(File file){
StringBuffer englishWord=new StringBuffer();
try{
FileReader inOne=【代码1】//创建指向文件f的inOne的对象
BufferedReader inTwo=【代码2】//创建指向文件inOne的inTwo的对象
import java.awt.event.*;
import java.io.*;
import javax.sound.sampled.*;
import javax.swing.*;
import java.util.regex.*;
public class StudyFrame extends JFrame implements ItemListener,ActionListener,Runnable{
BufferedReader类创建的对象称为缓冲输入流,该输入流的指向必须是一个Reader流,称作BufferedReader流的底层流,底层流负责将数据读入缓冲区,BufferedReader流的源就是这个缓冲区,缓冲输入流再从缓冲区中读取数据。
BufferedWriter类创建的对象称为缓冲输出流,缓冲输出流可以将BufferedWriter流和FileWriter流连接在一起,然后使用BufferedWriter流将数据写入缓冲区,FileWriter流作为BufferedWriter的底层流,负责将数据写入最终目的地。
pNorth.add(choice);
add(pNorth,BorderLayout.NORTH);
JPanel pSouth=new JPanel();
pSouth.add(getWord);
pSouth.add(voiceWord);
add(pNorth,BorderLayout.SOUTH);
}
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的对象
choice.addItemListener(this);
voiceWord.addActionListener(this);
getWord.addActionListener(this);
JPanel pNorth=new JPanel();
pNorth.add(new JLabel("选择一个英文单词组成的文件"));
BufferedReader inTwo=【代码5】//创建指向文件inOne的inTwo的对象
String s=null;
while((s=inTwo.readLine())!=null)
{
textHelp.append(s+"\n");
}
inOne.close();
inTwo.close();
}
pCenter=new JPanel();
mycard=new CardLayout();
pCenter.setLayout(mycard);
textHelp=new JTextArea();
pCenter.add("word",showWord);
pCenter.add("help",textHelp);
public static void main(String args[]){
new StudyFrame();
}
}
模板2:EnglishWord.java
import java.io.*;
import java.util.StringTokenizer;
public class EnglishWord{
setSize(350,220);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
validate();
String regex="\\s*[a-zA-Z]+\\s{0,}";//匹配单词的正则表达式
p=pile(regex,Pattern.MULTILINE);//初始化模式对象
mycard.show(pCenter,"word");
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource()==getWord)
{
boolean boo=m.find();
if(boo)
{
trainedWord=m.group();
showWord.setFont(new Font("宋体",Font.BOLD,24));
showWord.setBackground(Color.green);
getWord=new JButton("下一个单词");
voiceWord=new JButton("发音");
voiceThread=new Thread(this);
showWord.setText(trainedWord);
}
else
{
showWord.setText("训练完毕");
}
}
if(e.getSource()==voiceWord)
{
if(!(voiceThread.isAlive()))
{
voiceThread=new Thread(this);
FileReader类是Reader的子类,该类创建的对象称为文件字符输入流。文件字符输入流按字符读取文件中的数据。FileReader流按顺序读取文件,只要不关闭流,每次调用读取方法时FileReader流就会依次读取文件中其余的内容,直到文件的末尾或流被关闭。
FileWriter类是Writer的子类,该类创建的对象称为文件字符输出流。文件字符输出流按字符将数据写入到文件中。FileWriter流按顺序写入数据,只要不关闭流,每次调用写入方法时FileWriter流就会依次向文件写入内容,直到流被关闭。
CardLayout mycard;
JTextArea textHelp;
JMenuBar menubar;
JMenu menu;
JMenuItem help;
Pattern p;//模式对象
Matcher m;//匹配对象
public StudyFrame()
{
english=new EnglishWord();
(2)BufferedWriter对象调用newLine方法可向文件写入回行。
(3)播放声音可以使用javax.sound.sampled包中的AudioSystem类,该类调用static方法public Clip getClip()可以返回一个Clip对象,AudioSystem类调用static方法getAudioInputStream(File file)可以返回一个指向文件的AudioInputStream音频流对象,Clip对象调用void open(AudioInputStream stream)可以打开参数stream指定的音频流。
FileInputStream是InputStream的子类,该类创建的对象称为文件字节输入流。文件字节输入流按字节读取文件中的数据。FileInputStream流按顺序读取文件,只要不关闭流,每次调用读取方法时就依次读取文件中其余的内容,直到文件的末尾或流被关闭。
FileOutputStream类是OutputStream的子类,该类创建的对象称为文件字节输出流。文件字节输出流按字节将数据写入到文件中。FileOutputStream流按顺序写入数据,只要不关闭流,每次调用写入方法FileOutputStream流就会依次向文件写入内容,直到流被关闭。
clip.open(AudioSystem.getAudioInputStream(voiceFile));
}
catch(Exception exp){}
clip.start();
voiceWord.setEnabled(true);
}
}6.实验指导
(1)BufferedReader对象调用readLine方法可读取文件中的一行内容。
String s=null;
int i=0;
while((【代码3】)!=null)//inTwo读取一行
{
englishWord.append(" "+s+" ");
}ห้องสมุดไป่ตู้
}
catch(Exception e) {}
return englishWord;
}
}模板3:StudyFrame.java
相关主题