How to save a file to a chosen directory with JFil

2019-08-09 23:13发布

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

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?

Thanks in advance.

1条回答
Deceive 欺骗
2楼-- · 2019-08-09 23:28

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

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;
}

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.

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;
    }

}

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;
    }

}
查看更多
登录 后发表回答