首页 新闻 赞助 找找看

文件保存出错

0
悬赏园豆:5 [已解决问题] 解决于 2012-10-28 10:33

请看下面代码:

/*
 * 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中的文本保存到要保存的文件中时没能正确保存!

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

 

iGeneral的主页 iGeneral | 初学一级 | 园豆:191
提问于:2012-10-12 11:31
< >
分享
最佳答案
1

try {

fw.close();

bw.close();
}

改成:

try {

bw.close();

fw.close();

}

顺序反了,先关闭子类,再关闭父类

收获园豆:5
星空雾雨 | 菜鸟二级 |园豆:311 | 2012-10-15 16:25

非常感谢,如果没有你的帮忙,我真的没发现这个错误!

修改过后问题解决了,但是出现了新的问题:保存文件时没能保存textarea中的换行符,你知道是什么问题吗?

再次感谢!

iGeneral | 园豆:191 (初学一级) | 2012-10-18 21:35

@iGeneral: 

//set windows nature
this.setTitle("iGeneral NoteBook");
this.setSize(500, 400);
this.setLocation(400, 200);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jta.addKeyListener(new KeyAdapter(){
public void keyPressed(KeyEvent e){
if(e.getKeyCode()==KeyEvent.VK_ENTER)
{
jta.append("\r");
}
}
});

加上这行,自已理解一下

星空雾雨 | 园豆:311 (菜鸟二级) | 2012-10-19 09:11

@星空雾雨: 谢谢你,已经成功了!

但是,我一直不明白为什么你能知道这么做而我不知道呢?嘻嘻嘻。。。
可以结贴了!

iGeneral | 园豆:191 (初学一级) | 2012-10-28 10:32

@iGeneral: 傻孩子 ,多看书,少睡觉,多吃饭,少运动~~~~~~~~~

星空雾雨 | 园豆:311 (菜鸟二级) | 2012-10-28 10:36
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册