在 Java 中保存 FileDialog 去除初始文件扩展名

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5144547/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 09:41:24  来源:igfitidea点击:

Save FileDialog in Java strips initial file extension

javaawtfiledialog

提问by vocaro

I'm using java.awt.FileDialog to create a dialog for saving a file. The problem is that when I specify a suggested (default) file name, FileDialog strips its extension. Example:

我正在使用 java.awt.FileDialog 创建一个用于保存文件的对话框。问题是,当我指定一个建议的(默认)文件名时,FileDialog 会去掉它的扩展名。例子:

import java.awt.*;
import java.io.*;

public class SaveFile {
    public static void main(String[] args) {
        FileDialog fileDialog = new FileDialog(new Frame(), "Save", FileDialog.SAVE);
        fileDialog.setFilenameFilter(new FilenameFilter() {
            public boolean accept(File dir, String name) {
                return name.endsWith(".txt");
            }
        });
        fileDialog.setFile("Untitled.txt");
        fileDialog.setVisible(true);
        System.out.println("File: " + fileDialog.getFile());
    }
}

I would expect that when the FileDialog appears, the default file name is "Untitled.txt", but instead it is just "Untitled". When users click Save, I get back a filename without the extension. FileDialog does this on both Windows and OS X.

我希望当 FileDialog 出现时,默认文件名是“Untitled.txt”,但它只是“Untitled”。当用户单击“保存”时,我会返回一个没有扩展名的文件名。FileDialog 在 Windows 和 OS X 上都这样做。

I don't get it. Why would FileDialog deliberately strip the extension? Is there some logical reason for this? The documentation doesn't discuss it. As a workaround, I could simply add the extension to the string that FileDialog returns, but still, this seems like a bug...

我不明白。为什么 FileDialog 会故意去掉扩展名?这有什么合乎逻辑的原因吗?文档没有讨论它。作为一种解决方法,我可以简单地将扩展名添加到 FileDialog 返回的字符串中,但这仍然看起来像是一个错误......

(Note that I cannot use JFileChooser; I need the native AWT FileDialog.)

(请注意,我不能使用 JFileChooser;我需要本机 AWT FileDialog。)

采纳答案by Tom Quarendon

This doesn't happen for me, on Windows 7, with Sun Java 1.5 and 1.6.

在使用 Sun Java 1.5 和 1.6 的 Windows 7 上,这不会发生在我身上。

The behaviour I get depends slightly on the setting of the "Hide extensions of known file types" in Windows explorer. If that's on, then the I don't see the extension in the file dialog, as you might expect, but it does return me the full file name.

我得到的行为略微取决于 Windows 资源管理器中“隐藏已知文件类型的扩展名”的设置。如果打开,那么正如您所料,我在文件对话框中看不到扩展名,但它确实返回了完整的文件名。

EDIT: Realise I was wrong about AWT and native widgets -- confusing AWT and Swing.

编辑:意识到我对 AWT 和本机小部件的看法是错误的——混淆了 AWT 和 Swing。

回答by Gubatron

I've been looking for an answer to this very same problem which appears only on Mac. You either have to live with the ugly JFileChooser (swing, lightweight, not native look) option, or have an if (os is mac) and handle things differently by putting the file extension at the end yourself.

我一直在寻找这个仅出现在 Mac 上的相同问题的答案。您要么不得不忍受丑陋的 JFileChooser(swing,轻量级,非本机外观)选项,要么使用 if(os 是 mac)并通过自己将文件扩展名放在末尾来处理不同的事情。

It's a Mac Java AWT bug that will hopefully be fixed at some point.

这是一个 Mac Java AWT 错误,有望在某个时候得到修复。

回答by Dragos Roban

Here is an example how to save a new file to specified directory and name of file from FileDialog , Strings taken from a vector of Strings.It works for me !

这是一个示例,如何将新文件保存到 FileDialog 中的指定目录和文件名,从字符串向量中获取的字符串。它对我有用!

public static void SaveFileTo(Vector<String> myLines) {
        FileOutputStream f = null;
        DataOutputStream h = null;
        FileDialog d = new FileDialog(new JFrame(), "Save", FileDialog.SAVE);
        d.setVisible(true);
        String dir;
        dir = d.getDirectory();
        File oneFile = new File(dir + d.getFile());
        try {
            oneFile.createNewFile();
        } catch (IOException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
            f = new FileOutputStream(oneFile);
            h = new DataOutputStream(f);
            for (String de : myLines) {
                h.writeBytes(de);               
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally {
            try {
                h.close();
                f.close();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

回答by monster860

Use JFileChooser, but put that at the beginning of the program:

使用 JFileChooser,但将其放在程序的开头:

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }