使用 JFileChooser 设置默认保存扩展名

Set default saving extension with JFileChooser(使用 JFileChooser 设置默认保存扩展名)

本文介绍了使用 JFileChooser 设置默认保存扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 JFileChooser 保存文件.但是,我似乎遇到了一些麻烦.这是我的代码:

I'm trying to save a file using JFileChooser. However, I seem to be having some trouble with it. Here's my code:

    if (e.getSource() == saveMenu) {

        JFileChooser chooser = new JFileChooser();
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

        FileNameExtensionFilter xmlFilter = new FileNameExtensionFilter("xml files (*.xml)", "xml");
        // add filters
        chooser.addChoosableFileFilter(xmlFilter);
        chooser.setFileFilter(xmlFilter);

        int result = chooser.showSaveDialog(Simulation.this);

        if (result == chooser.APPROVE_OPTION) {
            writeToXML(chooser.getSelectedFile());
        }

    }

这并不强制文件具有 .xml 扩展名,因此我尝试使用以下代码强制文件以扩展名 .xml 保存

This doesn't force the file to have a .xml extension, so I've tried to use the following code to force the file to be saved with the extension .xml

     OutputFormat format = OutputFormat.createPrettyPrint();
     format.setEncoding("UTF-8");
     XMLWriter xmlWriter = null;
     try {              
         xmlWriter = new XMLWriter(new OutputStreamWriter(
                 new FileOutputStream(f+".xml"), "UTF8"),
                 format);

但是,这样我不能阻止用户在 JFileChooser 中编写 xpto.xml,如果他们这样做,文件将具有两个扩展名":它将是一个名为 xpto.xml.xml

However, with this I can't prevent the user from writing xpto.xml in the JFileChooser and if they do that, the file will have "two extensions": it will be a file named xpto.xml.xml

所以我的问题是:

  • 如何让JFileChooser默认保存一个xml文件?
  • 如果用户插入了像 xpto.xml 这样的文件名,如何将其保存为 xpto.xml 而不是 xpto.xml.xml?
  • How can I make the JFileChooser save an xml file by default?
  • If the user inserts a file name like xpto.xml, how can I save it as xpto.xml and not xpto.xml.xml?

推荐答案

如您所见,JFileChooser 不会在保存时强制执行 FileFilter.它将在它显示的对话框中灰显现有的非 XML 文件,但仅此而已.要强制执行文件名,您必须完成所有工作.(这不仅仅是 JFileChooser 糟糕的问题 - 这是一个需要处理的复杂问题.您可能希望您的用户能够将他们的文件命名为 xml.xml.xml.xml.)

As you've noticed, JFileChooser doesn't enforce the FileFilter on a save. It will grey-out the existing non-XML file in the dialog it displays, but that's it. To enforce the filename, you have to do all the work. (This isn't just a matter of JFileChooser sucking -- it's a complex problem to deal with. Your might want your users to be able to name their files xml.xml.xml.xml.)

在您的情况下,我建议使用 FilenameUtils 来自 Commons IO:

In your case, I recommend using FilenameUtils from Commons IO:

File file = chooser.getSelectedFile();
if (FilenameUtils.getExtension(file.getName()).equalsIgnoreCase("xml")) {
    // filename is OK as-is
} else {
    file = new File(file.toString() + ".xml");  // append .xml if "foo.jpg.xml" is OK
    file = new File(file.getParentFile(), FilenameUtils.getBaseName(file.getName())+".xml"); // ALTERNATIVELY: remove the extension (if any) and replace it with ".xml"
}

如果您想在保存对话框中使用多种类型,还有一些建议:如何使用 JFileChooser 保存文件?

There's also some ideas for what to do if you want multiple types in the save dialog here: How to save file using JFileChooser?

这篇关于使用 JFileChooser 设置默认保存扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:使用 JFileChooser 设置默认保存扩展名

基础教程推荐