1

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

    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);
                }
            }
        }
    }         
4

2 回答 2

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]);which 返回由File

更新...

您的文件写入过程也有点偏离。

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

您不应该关闭 中的资源try-catch,如果 中Exception发生an try-catchclose则永远不会调用方法,从而使资源保持打开状态...

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块尝试关闭所有资源,例如...

BufferedWriter bw = null;
try
{
    /*...*/
    FileWriter fw = new FileWriter(file.getAbsoluteFile());
    bw = new BufferedWriter(fw);
    PrintWriter out = new PrintWriter(bw);
    out.print(xmlTableProducer.getDynamicText());

    JOptionPane.showMessageDialog(this, "File Saved Successfully!", "Saved", JOptionPane.INFORMATION_MESSAGE);
}
catch(IOException xcp)
{
    xcp.printStackTrace(System.err);
} finally {
    try 
    {
        bw.close();
    } 
    catch    (Exception exp) {
    }
}
于 2013-08-22T00:47:41.290 回答
2

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

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

添加

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

验证您正在查看的目录是那里显示的路径..

于 2013-08-22T00:25:29.797 回答