Java JFileChooser getAbsoluteFile 添加文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2730246/
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
Java JFileChooser getAbsoluteFile Add File Extension
提问by iTEgg
i have this issue working but i would like to know if there is a better way of adding the file extension?
我有这个问题,但我想知道是否有更好的方法来添加文件扩展名?
what i am doing right now is:
我现在正在做的是:
String filePath = chooser.getSelectedFile().getAbsoluteFile() + ".html";
im adding the extension hard coded. and then saving to it.
我添加了硬编码的扩展。然后保存到它。
just wondering if there is a more robust/logical manner this can be implemented?
只是想知道是否有更健壮/合乎逻辑的方式可以实现?
thank you for your time.
感谢您的时间。
EDIT: i ask this as i would like my app to be portable across platforms. so adding .html manually i may make this a windows only solution.
编辑:我问这个是因为我希望我的应用程序可以跨平台移植。所以手动添加 .html 我可能会使其成为仅限 Windows 的解决方案。
EDIT: i think ive surfed enough to know that .html hard coded is safe as i havent found any documentation that says dont take this approach (not completely sure).
编辑:我认为我已经足够了解 .html 硬编码是安全的,因为我还没有找到任何说明不要采用这种方法的文档(不完全确定)。
ISSUE: also if i want to save the file in another format, text, for example how do i detect that the user selected which format?
问题:另外,如果我想以另一种格式保存文件,例如文本,我如何检测用户选择了哪种格式?
FileNameExtensionFilter can add filters to the dialog but how do i get the return value for file type selected?
FileNameExtensionFilter 可以向对话框添加过滤器,但如何获取所选文件类型的返回值?
EDIT: i have studied thisbut still unclear how to retrive user selected file type.
编辑:我已经研究过这个,但仍然不清楚如何检索用户选择的文件类型。
EDIT: this is a rephrase of my issue:
编辑:这是我的问题的改写:
alt text http://img98.imageshack.us/img98/4904/savef.jpgmy question is how can i retrieve/find out which one of the two filters the user has selected as the save format. HTML or JPEG? how do i retrieve this info from JFileChooser? thank you.
替代文本 http://img98.imageshack.us/img98/4904/savef.jpg我的问题是如何检索/找出用户选择作为保存格式的两个过滤器中的哪一个。HTML 还是 JPEG?我如何从 JFileChooser 中检索此信息?谢谢。
EDIT: found something out: it has something to do with JFileChooser.getFileFilter() your help still welcome.
编辑:发现了一些东西:它与 JFileChooser.getFileFilter() 有关,仍然欢迎您的帮助。
EDIT: getFileFilter() and FileNameExtensionFilter comparasion solved this issue.
编辑: getFileFilter() 和 FileNameExtensionFilter 比较解决了这个问题。
采纳答案by iTEgg
Here is the code snippet that solves the issue:
这是解决问题的代码片段:
JFileChooser chooser = new JFileChooser();
chooser.setMultiSelectionEnabled(false);
chooser.setAcceptAllFileFilterUsed(false);
FileNameExtensionFilter filter = new FileNameExtensionFilter("HTML Documents", "htm", "html");
chooser.setFileFilter(filter);
int option = chooser.showSaveDialog(ChatGUI.this);
if (option == JFileChooser.APPROVE_OPTION) {
// Set up document to be parsed as HTML
StyledDocument doc = (StyledDocument)textPaneHistory.getDocument();
HTMLEditorKit kit = new HTMLEditorKit();
BufferedOutputStream out;
try {
System.out.println(chooser.getFileFilter());
if (chooser.getFileFilter() == filter)
System.out.println("ha ha");
}
}
回答by Jean-Fran?ois
You're probably looking for this:
你可能正在寻找这个:
The trick consists in casting the returned FileFilter to FileNameExtensionFilter and then apply getExtensions().
技巧在于将返回的 FileFilter 转换为 FileNameExtensionFilter,然后应用 getExtensions()。
JFileChooser fileChooser = new JFileChooser("");
// Prevent user to use the default All Files option
fileChooser.setAcceptAllFileFilterUsed(false);
[...]
// Get the FileFilter
FileFilter ff = fileChooser.getFileFilter();
// Cast the FileFilter to FileNameExtensionFilter
FileNameExtensionFilter extFilter = (FileNameExtensionFilter)ff;
// Get the Extension
String ext = extFilter.getExtensions()[0];
Or, for making it compact:
或者,为了使其紧凑:
ext = ((FileNameExtensionFilter)fileChooser.getFileFilter()).getExtensions()[0];
回答by Mia Clarke
I don't understand what it is you're trying to do. Are you trying to save the selected file in some other format than it already is of? The path of the selected file will contain the file extension, so you don't need to add it manually. The following, for example, will print "/Users/banang/Documents/anything.html" to the screen if the file anything.html is selected.
我不明白你想做什么。您是否尝试以其他格式保存所选文件?所选文件的路径将包含文件扩展名,因此您无需手动添加。例如,如果文件anything.html 被选中,以下内容将在屏幕上打印“/Users/banang/Documents/anything.html”。
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
System.err.println(chooser.getSelectedFile().getCanonicalPath());
Please try to clarify your question a bit.
请尝试澄清一下您的问题。

