将所有部分放在一起并非易事。您需要一个 FileFilter 来保存到特定的扩展名。
这是我的 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;
}
您会注意到该方法中有代码确保 EXTENSION 附加在文件名的末尾,除非 EXTENSION 已经存在。这是 Java 等效项中缺少的标准 Windows 行为。
为了保存特定的扩展,您需要一个 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;
}
}
最后是 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;
}
}