修改文件名时,文件无法保存

时间:2013-08-22 00:05:30

标签: java swing file file-extension

我有一个按钮,打开一个保存对话框窗口,其中设置了默认扩展过滤器,但是当用户没有在文件名上提供文件扩展名时,它应该自动添加扩展名。问题是,当发生这种情况时,文件将不会保存(或无法保存),但不会抛出任何异常。文件保存成功弹出窗口显示告诉用户文件已成功保存但目录中未找到文件。这是我的代码:

    private void saveRecordsButtonActionPerformed(java.awt.event.ActionEvent evt)                                                  
    {                                                      
        if(evt.getSource() == this.saveRecordsButton)
        {
            String recordName = JOptionPane.showInputDialog(this, "Please type in the name of the record you are about to export: ", "Input Notice", JOptionPane.INFORMATION_MESSAGE);
            if(recordName == null || recordName.equals(""))
            {
                JOptionPane.showMessageDialog(this, "You must type in the name of the record in order to save!", "Input Error!", JOptionPane.ERROR_MESSAGE);
                return;
            }

            int returnVal = this.fileChooser.showSaveDialog(this);
            if(returnVal == JFileChooser.APPROVE_OPTION) 
            {
                //ObjectOutput oos = null;
                try
                {
                    File file = this.fileChooser.getSelectedFile();
                    String recordDate = this.viewByDateCB.getSelectedItem().toString();
                    XMLTableProducer xmlTableProducer = new XMLTableProducer(this.cbtm, "Cash Book Table", recordName, recordDate, new Date());

                    if(!file.getName().contains("."))
                    {
                        FileNameExtensionFilter filter = (FileNameExtensionFilter)this.fileChooser.getFileFilter();
                        file = new File(file.getName()+(filter.getExtensions())[0]);
                        System.out.println(file.getName()); //This actually prints out the exact file name with extension the way I want
                    }

                    // if file doesnt exists, then create it
                    if(!file.exists()) 
                    {
                        file.createNewFile();
                    }

                    FileWriter fw = new FileWriter(file.getAbsoluteFile());
                    BufferedWriter bw = new BufferedWriter(fw);
                    PrintWriter out = new PrintWriter(bw);
                    out.print(xmlTableProducer.getDynamicText());
                    out.close();
                    bw.close();

                    JOptionPane.showMessageDialog(this, "File Saved Successfully!", "Saved", JOptionPane.INFORMATION_MESSAGE);
                }
                catch(IOException xcp)
                {
                    xcp.printStackTrace(System.err);
                }
            }
        }
    }         

2 个答案:

答案 0 :(得分:2)

代码看起来还不错。由于您没有看到任何异常,我怀疑您没有查看正确的目录。之后

// if file doesn't exists, then create it
                        if(!file.exists()) 
                        {
                            file.createNewFile();
                        }

添加

System.out.println(file.getAbsolutePath());

验证您要查看的目录是否为此处显示的路径..

答案 1 :(得分:2)

file = new File(file.getName()+(filter.getExtensions())[0]);正在剥离File ...

的路径

假设用户选择将文件保存在C:\My Documents\Boss中。当您File#getName时,它只会返回Boss。现在这意味着文件将保存到执行程序的相同位置(即.\Bosss

而不是file = new File(file.getName()+(filter.getExtensions())[0]);,您应该使用file = new File(file.getPath()+(filter.getExtensions())[0]);来返回“{1}}

所代表的”完整“路径和文件名

<强>更新...

您的文件编写过程也有点过时了。

一般的经验法则,如果你打开流,你应该关闭它......

您不应该关闭File中的资源,如果try-catch中出现Exception,则永远不会调用try-catch方法,只留下资源打开...

close

相反,您应该使用try { /*...*/ FileWriter fw = new FileWriter(file.getAbsoluteFile()); BufferedWriter bw = new BufferedWriter(fw); PrintWriter out = new PrintWriter(bw); out.print(xmlTableProducer.getDynamicText()); out.close(); bw.close(); JOptionPane.showMessageDialog(this, "File Saved Successfully!", "Saved", JOptionPane.INFORMATION_MESSAGE); } catch(IOException xcp) { // If an exception occurs, the file will remain open xcp.printStackTrace(System.err); } 块来尝试关闭所有资源,例如......

finally