Java 中的“保存文件对话框”会去除初始文件扩展名

发布于 2024-10-19 11:30:14 字数 952 浏览 6 评论 0原文

我正在使用 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());
    }
}

我希望当 FileDialog 出现时,默认文件名是“Untitled.txt”,但实际上它只是“Untitled”。当用户单击“保存”时,我会返回一个不带扩展名的文件名。 FileDialog 在 Windows 和 OS X 上都执行此操作

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

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

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:

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.

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...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

◇流星雨 2024-10-26 11:30:14

在使用 Sun Java 1.5 和 1.6 的 Windows 7 上,这种情况不会发生。

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

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

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

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.

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

七禾 2024-10-26 11:30:14

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

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

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.

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

荭秂 2024-10-26 11:30:14

下面是一个示例,如何将新文件保存到 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();
            }
        }

    }

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 !

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

    }
·深蓝 2024-10-26 11:30:14

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

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

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

    try {
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
    } catch (Exception e) {
        e.printStackTrace();
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文