集册 Java实例教程 序列化对象

序列化对象

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

474
序列化对象

import java.io.ByteArrayOutputStream;

import java.io.FileOutputStream;

import java.io.IOException;
/* 
 来自 
*时 代 J a v a*/

import java.io.ObjectOutput;

import java.io.ObjectOutputStream;


public class Main {

  public void m() {

    Object object = new javax.swing.JButton("push me");


    try {

      // Serialize to a file

      ObjectOutput out = new ObjectOutputStream(new FileOutputStream(

          "filename.ser"));

      out.writeObject(object);

      out.close();

      /*
      N o w J a v a . c o m - 时代Java 提 供
      */

      // Serialize to a byte array

      ByteArrayOutputStream bos = new ByteArrayOutputStream();

      out = new ObjectOutputStream(bos);

      out.writeObject(object);

   
展开阅读全文