下是另存为按钮的处理方法当先关闭fw后关闭bw时,无论jta里写什么保存的txt都是空文件jta为JFrameTxetAreapublicvoidactionPerformed(ActionEvente){if(e.getActionCommand().equals(...
下是另存为按钮的处理方法 当先关闭fw后关闭bw时,无论jta里写什么保存的txt都是空文件
jta为JFrameTxetArea
public void actionPerformed(ActionEvent e) {
if(e.getActionCommand().equals("save")){
JFileChooser jfc=new JFileChooser();
jfc.setDialogTitle("另存为...");
jfc.showSaveDialog(null);
jfc.setVisible(true);
String file=jfc.getSelectedFile().getAbsolutePath();
FileWriter fw=null;
BufferedWriter bw=null;
try {
fw=new FileWriter(file);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
bw=new BufferedWriter(fw);
try {
bw.write(jta.getText());
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
finally{
try {
bw.close();
fw.close()
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
展开