java 如何使用 JFileChooser 将文件保存到选定的目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13920501/
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
How to save a file to a chosen directory with JFileChooser
提问by Gilbert Le Blanc
I'm new to Java so please bear with me.
我是 Java 新手,所以请耐心等待。
In my program, I want to give the user the ability to save a file to the directory of their choice. After doing a little research, I found this nifty class called JFileChooser. What I want to do is allow the user to go to their desired directory through the JFileChooser GUI, type a name for their file, and allow them to save their file to the desired directory. I tried looking online for a solution to how to do this but almost everywhere I read, the final answer was "Now you have to make your program save the file" which I have no idea how to do. Could someone provide some well commented dummy code that would do the above description? Also, does anyone know whether JFileChooser provides a "New Folder" option?
在我的程序中,我想让用户能够将文件保存到他们选择的目录中。在做了一些研究之后,我发现了这个名为 JFileChooser 的漂亮类。我想要做的是允许用户通过 JFileChooser GUI 进入他们想要的目录,为他们的文件输入一个名称,并允许他们将他们的文件保存到所需的目录。我尝试在网上寻找如何做到这一点的解决方案,但几乎在我读到的任何地方,最终答案都是“现在你必须让你的程序保存文件”,我不知道该怎么做。有人可以提供一些注释良好的虚拟代码来完成上述描述吗?另外,有谁知道 JFileChooser 是否提供“新建文件夹”选项?
Thanks in advance.
提前致谢。
回答by Gilbert Le Blanc
It's not trivial to put all of the pieces together. You need a FileFilter to save to particular extensions.
将所有部分放在一起并非易事。您需要一个 FileFilter 来保存到特定的扩展名。
Here's an example from one of my Swing applications.
这是我的一个 Swing 应用程序的示例。
protected static final String EXTENSION = ".png";
protected static final String FORMAT_NAME = "png";
protected static final LayoutFileFilter SAVE_AS_IMAGE =
new LayoutFileFilter("PNG Image Format", EXTENSION, true);
protected int chooseSaveFile(BufferedImage image) {
JFileChooser fileChooser = new JFileChooser();
ExtensionFileFilter pFilter = new ExtensionFileFilter(SAVE_AS_IMAGE);
fileChooser.setFileFilter(pFilter);
int status = fileChooser.showSaveDialog(frame.getFrame());
if (status == JFileChooser.APPROVE_OPTION) {
File selectedFile = fileChooser.getSelectedFile();
try {
String fileName = selectedFile.getCanonicalPath();
if (!fileName.endsWith(EXTENSION)) {
selectedFile = new File(fileName + EXTENSION);
}
ImageIO.write(image, FORMAT_NAME, selectedFile);
} catch (IOException e) {
e.printStackTrace();
}
}
return status;
}
You'll notice that there's code in the method to ensure that the EXTENSION is appended on the end of the file name, unless the EXTENSION already exists. This is standard Windows behavior that is missing in the Java equivalent.
您会注意到方法中的代码确保将 EXTENSION 附加到文件名的末尾,除非 EXTENSION 已经存在。这是 Java 等效项中缺少的标准 Windows 行为。
In order to save a particular extension, you'll need a FileFilter.
为了保存特定的扩展名,您需要一个 FileFilter。
import java.io.File;
import javax.swing.filechooser.FileFilter;
public class ExtensionFileFilter extends FileFilter {
protected LayoutFileFilter filter;
protected String description;
protected String[] extensions;
public ExtensionFileFilter(LayoutFileFilter filter) {
this(filter.getDescription(), filter.getExtension());
this.filter = filter;
}
public ExtensionFileFilter(String description, String extension) {
this(description, new String[] {extension});
}
public ExtensionFileFilter(String description, String[] extensions) {
if ((description == null) || (description.equals(""))) {
this.description = extensions[0] + " {" + extensions.length + "}";
} else {
this.description = description;
}
this.extensions = (String[]) extensions.clone();
toLower(this.extensions);
}
private void toLower(String[] extensions) {
for (int i = 0, n = extensions.length; i < n; i++) {
extensions[i].toLowerCase();
}
}
@Override
public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
String path = file.getAbsolutePath().toLowerCase();
for (int i = 0, n = extensions.length; i < n; i++) {
String extension = extensions[i];
if (path.endsWith(extension)) {
return true;
}
}
}
return false;
}
@Override
public String getDescription() {
return description;
}
public LayoutFileFilter getLayoutFileFilter() {
return filter;
}
}
And finally, the LayoutFileFilter.
最后是 LayoutFileFilter。
public class LayoutFileFilter {
boolean isDefault;
String description;
String extension;
public LayoutFileFilter() {
}
public LayoutFileFilter(String description, String extension,
boolean isDefault) {
this.description = description;
this.extension = extension;
this.isDefault = isDefault;
}
public boolean isDefault() {
return isDefault;
}
public void setDefault(boolean isDefault) {
this.isDefault = isDefault;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getExtension() {
return extension;
}
public void setExtension(String extension) {
this.extension = extension;
}
}