Here you can find the source of saveObject(final Object content, final String description, final String extension, final File file)
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 |
public static String saveObject(final Object content, final String description, final String extension, final File file)
//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; } } }