文件保存出错

请看下面代码:

/*
 * NoteBook development
 * iGeneral
 * 2012.10.12
 * step:
 * 1.JFrame+JMenuBar+JMenu+JMenuItem
 * 2.ActionListon
 */
package lesson46;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.*;
public class NoteBook extends JFrame implements ActionListener {
    //default widget
    JMenuBar jmb=null;
    JMenu jmn=null;
    JMenuItem jmiOpen=null;
    JMenuItem jmiSave=null;
    JTextArea jta=null;
    public static void main(String[] args) {
        NoteBook noteBook=new NoteBook();
    }
    public NoteBook(){
        //create widget
        jta=new JTextArea();
        jmb=new JMenuBar();
        jmn=new JMenu("File");
        //set memory word
        jmn.setMnemonic('F');
        jmn.setToolTipText("Operate File");
        //ImageIcon imgOpen=new ImageIcon("btnOpen.png");
        jmiOpen=new JMenuItem("Open", new ImageIcon("./btnOpen.png"));
        //jmiOpen.setText("111");
        jmiOpen.setMnemonic('O');
        jmiOpen.addActionListener(this);
        jmiOpen.setActionCommand("File_Open");
        jmiSave=new JMenuItem("Save",new ImageIcon("images/btnSave.png"));
        jmiSave.setMnemonic('S');
        jmiSave.addActionListener(this);
        jmiSave.setActionCommand("File_Save");
        
        jmn.add(jmiOpen);
        jmn.add(jmiSave);
        jmb.add(jmn);
        this.setJMenuBar(jmb);
        this.add(jta);
        
        //set windows nature
        this.setTitle("iGeneral NoteBook");
        this.setSize(500, 400);
        this.setLocation(400, 200);
        this.setVisible(true);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
    //Override actionPerformed()
    public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("File_Open")){
            //System.out.println("Open");
            JFileChooser jfc=new JFileChooser();
            //以默认形式的打开文件的对话框
            jfc.showOpenDialog(null);
            jfc.setDialogTitle("Open...");
            jfc.setVisible(true);
            //get opened file's absolutePath
            String filePath=jfc.getSelectedFile().getAbsolutePath();
            //字符流
            FileReader fr=null;
            BufferedReader br=null;
            try {
                fr=new FileReader(filePath);
                br=new BufferedReader(fr);
                String s="";
                String allS="";
                while((s=br.readLine())!=null){
                    if(!allS.equals("")){
                        allS+="\r\n";
                    }
                    allS+=s;
                }
                this.jta.setText(allS);
            } catch (Exception e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }finally{
                try {
                    fr.close();
                    br.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
            }
        }
        if(e.getActionCommand().equals("File_Save")){
            JFileChooser jfc=new JFileChooser();
            jfc.showSaveDialog(null);
            jfc.setDialogTitle("Save as...");
            jfc.setVisible(true);
            
            String saveFilePath=jfc.getSelectedFile().getAbsolutePath();
            //System.out.println(saveFilePath);
            FileWriter fw=null;
            BufferedWriter bw=null;
            try {
                fw=new FileWriter(saveFilePath);
                bw=new BufferedWriter(fw);
                //System.out.println(this.jta.getText());
                bw.write(this.jta.getText());
            } catch (Exception e2) {
                // TODO: handle exception
                e2.printStackTrace();
            }finally{
                try {
                    fw.close();
                    bw.close();
                } catch (Exception e3) {
                    // TODO: handle exception
                    e3.printStackTrace();
                }
            }
            
        }
        
    }

}

不清楚原因,在将JTextArea中的文本保存到要保存的文件中时没能正确保存!

请大家帮忙解决,代码是没有出错的,逻辑也没错呀!到底是怎么回事,想了很久还是没能解决!

 

撒科打诨
浏览 763回答 1
1回答

四季花海

try { fw.close(); bw.close();} 改成: try { bw.close(); fw.close(); } 顺序反了,先关闭子类,再关闭父类
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java