集册 Java实例教程 将对象序列化为文件

将对象序列化为文件

欢马劈雪     最近更新时间:2020-01-02 10:19:05

534
将对象序列化到文件


import java.awt.Color;// 来自 时 代 J a v a 公 众 号

import java.awt.image.BufferedImage;

import java.io.*;

import java.util.ArrayList;

import java.util.logging.*;


public class Main{

    /**

     * Serialize an object to a file

     * @param object The object to serialize.

     * @param filename The filename of the serialized object

     */

    public static void serializeObject(Object object, String filename) {

        System.out.println("Serializing " + filename);

        try {

            FileOutputStream fileOut = new FileOutputStream(filename);

            ObjectOutputStream out = new ObjectOutputStream(fileOut);

            out.writeObject(object);

            out.close();

            fileOut.close();

        } catch (IOException ex) {

            Logger.getLogger(HelperFunctions.
展开阅读全文