使用添加的文件扩展名保存现有文件不提示覆盖消息

Saving already existing file with added file extension not prompting overwrite message

对于我正在做的 GUI 项目,我试图很好地降低保存机制。我目前正在研究覆盖文件,但 运行 遇到了一些问题。

这是我创建的测试用例:

Save current file using Save As. When prompted for a file name, give an existing file name with an added file extension.

假设调用了一个已经存在的文件:test.config

如果我输入:test然后按保存,会弹出一个覆盖提示,这就是我想要的。

如果我输入:test.config并按保存,NO弹出提示覆盖,程序继续。这不是我想要的。我想要一个覆盖提示弹出并警告用户。

我一直在各种网站和 Javadoc 中搜索文件,但我似乎无法弄清楚如果用户输入额外的扩展名,如何检查文件是否存在。

这里有一些代码可以帮助你们理解:

...

int returnVal = fc.showSaveDialog(Dialogc.this);
if(returnVal != JFileChooser.APPROVE_OPTION) {
        JOptionPane.showMessageDialog(null,
            "Error saving file. \nFile not saved.");
        return 1;
}

// Get file name
file = fc.getSelectedFile();

/* Check if file exists */
if(file.exists()) {
    JOptionPane.showConfirmDialog(null, 
        "File already exists. Overwrite?", "Overwrite Prompt",  
        JOptionPane.YES_NO_OPTION);
}
else {
    // If it doesn't exist, create new file
}
...

注意:我确实有添加或删除扩展名的方法。我是否应该使用这些来帮助检查给定文件是否已经存在?

覆盖approveSelection(...)方法以确认覆盖文件:

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class FileChooserSave
{
    private static void createAndShowUI()
    {
        final JFileChooser chooser = new JFileChooser( new File(".") )
        {
            public void approveSelection()
            {
                if (getSelectedFile().exists())
                {
                    int n = JOptionPane.showConfirmDialog(
                        this,
                        "Do You Want to Overwrite File?",
                        "Confirm Overwrite",
                        JOptionPane.YES_NO_OPTION);

                    if (n == JOptionPane.YES_OPTION)
                        super.approveSelection();

                }
                else
                    super.approveSelection();
            }
        };

        chooser.setSelectedFile( new File("something.rob") );
        int returnVal = chooser.showSaveDialog(null);


        if(returnVal == JFileChooser.APPROVE_OPTION)
        {
           System.out.println(chooser.getSelectedFile() );
        }
    }

    public static void main(String[] args)
    {
        EventQueue.invokeLater(new Runnable()
        {
            public void run()
            {
                createAndShowUI();
            }
        });
    }
}

我遇到了相反的问题...如果用户不输入扩展名,它就不会提示。如果用户没有输入,我会稍后添加扩展名。因此,在您的情况下,如果用户键入 "test",我的代码会将文件保存为 "test.config"。我假设你的工作方式相同?

为了修复它,我还必须在批准期间添加扩展...所以首先:

final JFileChooser fc = new JFileChooser(){
@Override
public void approveSelection(){

    File f = getSelectedFile();
            String filestring = f.toString();

            String[] left_side_of_dot = filestring.split("\.");

             f = new File(left_side_of_dot[0] + ".config");


    if(f.exists() && getDialogType() == SAVE_DIALOG){
        int result = JOptionPane.showConfirmDialog(YOUR_Frame,"The file exists, overwrite?","Existing file",JOptionPane.YES_NO_CANCEL_OPTION);
        switch(result){
            case JOptionPane.YES_OPTION:
                super.approveSelection();
                return;
            case JOptionPane.NO_OPTION:
                return;
            case JOptionPane.CLOSED_OPTION:
                return;
            case JOptionPane.CANCEL_OPTION:
                cancelSelection();
                return;
        }
    }
    super.approveSelection();
 }
};

然后在保存之前:

String noxtfilename = filename.substring(0, filename.length()-7);
    File file = new File(noxtfilename);
    fc.setSelectedFile(file); }
 FileNameExtensionFilter filefilter = new  FileNameExtensionFilter("Config File (*.config)","config");

 fc.setFileFilter(filefilter);


    int returnVal = fc.showSaveDialog(YOUR_Frame);

        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            String filestring = file.toString();

            String[] left_side_of_dot = filestring.split("\.");

             file = new File(left_side_of_dot[0] + ".config");