如何使用JFileChooser将文件保存到所选目录 [英] How to save a file to a chosen directory with JFileChooser

查看:52
本文介绍了如何使用JFileChooser将文件保存到所选目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Java的新手,所以请多多包涵.

I'm new to Java so please bear with me.

在我的程序中,我想让用户能够将文件保存到他们选择的目录中.经过一些研究,我发现了这个漂亮的类叫做JFileChooser.我想做的是允许用户通过JFileChooser GUI转到所需目录,为文件键入一个名称,然后允许他们将文件保存到所需目录.我试图在网上寻找解决方法,但是几乎在我读过的所有地方,最终答案都是现在您必须使程序保存文件",我不知道该怎么做.有人可以提供一些经过注释的伪代码来执行上述描述吗?另外,有人知道JFileChooser是否提供新文件夹"选项吗?

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?

先谢谢了.

推荐答案

将所有部分放在一起并不是一件容易的事.您需要一个FileFilter才能保存到特定的扩展名.

It's not trivial to put all of the pieces together. You need a FileFilter to save to particular extensions.

这是我的一个Swing应用程序中的一个示例.

Here's an example from one of my Swing applications.

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已经存在.这是标准的Windows行为,Java等效项中没有.

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.

为了保存特定的扩展名,您需要一个FileFilter.

In order to save a particular extension, you'll need a 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.

And finally, the 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;
    }

}

这篇关于如何使用JFileChooser将文件保存到所选目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆