Converts an object to a base64 encoded string - Android File Input Output

Android examples for File Input Output:Object Serialization

Description

Converts an object to a base64 encoded string

Demo Code


//package com.java2s;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import java.io.ObjectOutputStream;

import android.util.Base64;

public class Main {
    /**//w  w w.  j  a v a2s .  c  o  m
     * Converts an object to a base64 encoded string
     *
     * @param o The object to convert
     * @return The Base64 encoded string
     * @throws IOException
     */
    public static String objectToString(Object o) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        oos.writeObject(o);
        oos.close();
        return Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
    }
}

Related Tutorials