看看书上的就有.如:
class OpenHandler implements ActionListener{
public void actionPerformed(ActionEvent e){
JFileChooser c=new JFileChooser();
int rVal=c.showOpenDialog(WordPad.this);
if(rVal==JFileChooser.APPROVE_OPTION){
File dir=c.getCurrentDirectory();
File file=c.getSelectedFile();
t.setText(read(new File(dir,file.getName())));
}
}
private String read(File file){
try{
BufferedReader reader=new BufferedReader(new InputStreamReader(new FileInputStream(file),"GBK"));
String data=null;
StringBuffer buffer=new StringBuffer();
while((data=reader.readLine())!=null){
buffer.append(data+"\n");
}
reader.close();
return buffer.toString();
}catch(IOException e){
throw new RuntimeException(e);
}
}
}