Java 中的“保存文件对话框”会去除初始文件扩展名
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在使用 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.
我一直在寻找这个仅出现在 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.
下面是一个示例,如何将新文件保存到 FileDialog 中的指定目录和文件名,字符串取自字符串向量。它对我有用!
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 !
使用 JFileChooser,但将其放在程序的开头:
Use JFileChooser, but put that at the beginning of the program: