Android

Class, Object String form(Serializable)으로 save 하기

Leo 리오 2011. 6. 10. 01:47
반응형

/** Read the object from Base64 string. */
    public static Object S2Object( String s ) {
        byte [] data = Base64.decode(s, 0);
        ObjectInputStream ois;
        Object o=null;
        try
        {
            ois = new ObjectInputStream(new ByteArrayInputStream(data));
            o  = ois.readObject();
            ois.close();
        } catch (ClassNotFoundException e)
        {
            System.out.println("S2Object ClassNotFoundException failed");
            return null;
        } catch (IOException e)
        {
            System.out.println("S2Object IOException failed");
            return null;
        }
        return o;
    }
    /** Write the object to a Base64 string. */
    public static String O2String( Serializable o ) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos;
        try
        {
            oos = new ObjectOutputStream( baos );
            oos.writeObject( o );
            oos.close();
        } catch (IOException e)
        {
            System.out.println("writeObejct failed");
            e.printStackTrace();
            return "";
        }        
        return new String( Base64.encode( baos.toByteArray(),0 ) );
    }


implements Serializable 를 해야한다.


반응형