object Serializable To String - Android java.io

Android examples for java.io:Serializable

Description

object Serializable To String

Demo Code

import android.content.Context;
import java.io.ByteArrayOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Main{

    public static String objectSerializableToString(Serializable object) {
        ByteArrayOutputStream baos = null;
        ObjectOutputStream os = null;
        try {/* ww  w  . ja  v  a  2s. com*/
            if (object != null) {
                baos = new ByteArrayOutputStream();
                os = new ObjectOutputStream(baos);
                os.writeObject(object);
                os.flush();
                baos.flush();
                return baos.toString("UTF-8");
            }
        } catch (Throwable e) {
        } finally {
            try {
                if (os != null) {
                    os.close();
                }
            } catch (Throwable e2) {
            }

            try {
                if (baos != null) {
                    baos.close();
                }
            } catch (Throwable e2) {
            }
        }
        return null;
    }

}

Related Tutorials