serializationstorageformat.java

来自「开源(Open Source)项目JHotDraw的文档和源程序」· Java 代码 · 共 81 行

JAVA
81
字号
/*
 * @(#)SerializationStorageFormat.java 5.2
 *
 */

package CH.ifa.draw.util;

import javax.swing.filechooser.FileFilter;
import java.io.*;
import CH.ifa.draw.framework.Drawing;

/**
 * A SerializationStorageFormat is a straight-forward file format to store and restore
 * Drawings. It uses Java's serialization mechanism to store Drawings. The SerializationStorageFormat
 * has the file extension "ser" (e.g. my_picasso.ser).
 *
 * @author Wolfram Kaiser
 */
public class SerializationStorageFormat extends StandardStorageFormat {

	/**
	 * Create a SerialzationStorageFormat for storing and restoring Drawings.
	 */
	public SerializationStorageFormat() {
		super();
	}

	/**
	 * Factory method to create the file extension recognized by the FileFilter for this
	 * SerializationStorageFormat. The SerializationStorageFormat has the file extension "ser"
	 * (e.g. my_picasso.ser).
	 *
	 * @return new file extension
	 */
	protected String createFileExtension() {
		return "ser";
	}

	/**
	 * Factory method to create a file description for the file type when displaying the
	 * associated FileFilter.
	 *
	 * @return new file description
	 */
	public String createFileDescription() {
		return "Serialization (" + getFileExtension() + ")";
	}
	
	/**
	 * Store a Drawing under a given name. The name should be valid with regard to the FileFilter
	 * that means, it should already contain the appropriate file extension.
	 *
	 * @param fileName file name of the Drawing under which it should be stored
	 * @param saveDrawing drawing to be saved
	 */
	public String store(String fileName, Drawing saveDrawing) throws IOException {
		FileOutputStream stream = new FileOutputStream(adjustFileName(fileName));
		ObjectOutput output = new ObjectOutputStream(stream);
		output.writeObject(saveDrawing);
		output.close();
		return adjustFileName(fileName);
	}

	/**
	 * Restore a Drawing from a file with a given name. The name must be should with regard to the
	 * FileFilter that means, it should have the appropriate file extension.
	 *
	 * @param name of the file in which the Drawing has been saved
	 * @return restored Drawing
	 */
	public Drawing restore(String fileName) throws IOException {
		try {
			FileInputStream stream = new FileInputStream(fileName);
			ObjectInput input = new ObjectInputStream(stream);
			return (Drawing)input.readObject();
		}
		catch (ClassNotFoundException exception) {
			throw new IOException("Could not restore drawing '" + fileName +"': class not found!");
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?