Java Object Serialize serialize(Serializable obj, String fileName)

Here you can find the source of serialize(Serializable obj, String fileName)

Description

serialize

License

Apache License

Declaration

public static void serialize(Serializable obj, String fileName) 

Method Source Code


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

import java.io.*;

public class Main {

    public static void serialize(Serializable obj, String fileName) {
        ObjectOutputStream oos = null;
        try {/*from w  ww.  j a v  a  2 s. c o  m*/
            FileOutputStream out = new FileOutputStream(fileName);
            oos = new ObjectOutputStream(out);
            oos.writeObject(obj);
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static byte[] serialize(Serializable obj) {
        ObjectOutputStream oos = null;
        try {
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            oos = new ObjectOutputStream(out);
            oos.writeObject(obj);
            return out.toByteArray();
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

Related

  1. serialize(Serializable o)
  2. serialize(Serializable obj)
  3. serialize(Serializable obj)
  4. serialize(Serializable obj)
  5. serialize(Serializable obj)
  6. serialize(Serializable obj, String path)
  7. serialize(Serializable object)
  8. serialize(Serializable object)
  9. serialize(Serializable object)