Java JFileChooser saveObject(final Object content, final String description, final String extension, final File file)

Here you can find the source of saveObject(final Object content, final String description, final String extension, final File file)

Description

Saves a file with a file chooser

License

LGPL

Parameter

Parameter Description
content - The object to save
description - The description of the file
extension - The extension of the file
file - The file in where to save

Return

The path of the saved file

Declaration

public static String saveObject(final Object content, final String description, final String extension,
        final File file) 

Method Source Code

//package com.java2s;
//License from project: LGPL 

import java.io.File;

import java.io.FileOutputStream;

import java.io.ObjectOutputStream;

import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import javax.swing.filechooser.FileNameExtensionFilter;

public class Main {
    /**//  ww  w .  j  a  v  a 2 s.  c  o  m
     * Saves a file with a file chooser
     * 
     * @param content
     *            - The object to save
     * @param description
     *            - The description of the file
     * @param extension
     *            - The extension of the file
     * @param file
     *            - The file in where to save
     * @return The path of the saved file
     */
    public static String saveObject(final Object content, final String description, final String extension,
            final File file) {
        final JFileChooser fileChooser = new JFileChooser();
        final FileNameExtensionFilter langFilter = new FileNameExtensionFilter(description, extension);
        fileChooser.setFileFilter(langFilter);
        fileChooser.setSelectedFile(file);
        String path = "";
        try {
            if (fileChooser.showSaveDialog(null) == JFileChooser.APPROVE_OPTION) {
                path = fileChooser.getSelectedFile().getAbsolutePath();
                if (!path.endsWith("." + extension)) {
                    path += "." + extension;
                }
                final File file2 = new File(path);
                if ((file2.exists() && JOptionPane.OK_OPTION == JOptionPane.showConfirmDialog(null,
                        "The file exists, do you want to replace it?", "File Exists", JOptionPane.YES_NO_OPTION))
                        || !file2.exists()) {
                    final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(path));
                    oos.writeObject(content);
                    oos.close();
                }
            }
            return path;
        } catch (final Exception e) {
            e.printStackTrace();
            return path;
        }
    }
}

Related

  1. retrieveFilesFromDir(File basedir, FileFilter filter)
  2. run_file_chooser(String file_desc, String extension)
  3. save(JFileChooser fileChooser, Runnable action)
  4. saveFile(Component parent, File defaultFile)
  5. saveFile(String title, File currentDir, javax.swing.filechooser.FileFilter filter)
  6. saveSystemFiles(Component owner)
  7. saveToFile(Object obj)
  8. select_file(Component parent, boolean show_save)
  9. selectAndSaveToFile(String content, Component parent)