Object 및 String 전환

1909 단어
Object는 시리얼화된 객체여야 합니다.
//  list 
    public static String list2String(List<ShopcarBean> list) throws IOException {
        //  ByteArrayOutputStream , 
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        //  ObjectOutputStream
        ObjectOutputStream oos = new ObjectOutputStream(baos);
        // writeObject  , readObject 
        oos.writeObject(list);
        //  , Base64.encode Base64 , String 
        String listString = new String(Base64.encode(baos.toByteArray(),
                Base64.DEFAULT));
        //  oos
        oos.close();
        return listString;
    }

    //  list 
    public static List<ShopcarBean> string2List(String str)
            throws StreamCorruptedException, IOException,
            ClassNotFoundException {
        byte[] mByte = Base64.decode(str.getBytes(), Base64.DEFAULT);
        ByteArrayInputStream bais = new ByteArrayInputStream(mByte);
        ObjectInputStream ois = new ObjectInputStream(bais);
        List<ShopcarBean> stringList = (List<ShopcarBean>) ois.readObject();
        return stringList;
    }

좋은 웹페이지 즐겨찾기