日期:2014-05-20 浏览次数:20790 次
//将对象写入字符串 public static String serialzeToString(Object o){ ObjectOutputStream os = null; try{ ByteArrayOutputStream bs = new ByteArrayOutputStream(); os = new ObjectOutputStream(bs); os.writeObject(o); byte[] bytes = bs.toByteArray(); String retStr = new String(bytes); return retStr; }catch(Exception e){ e.printStackTrace(); return null; } }
//从字符串中读取出对象 public static Object derialzeFromString(String content){ ObjectInputStream ois = null; try{ byte[] bytes = content.getBytes(); ByteArrayInputStream bs = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bs); Object o = ois.readObject(); return o; }catch(Exception e){ e.printStackTrace(); return null; } }
//将对象写入字符串 public static String serialzeToString(Object o){ ObjectOutputStream os = null; try{ ByteArrayOutputStream bs = new ByteArrayOutputStream(); os = new ObjectOutputStream(bs); os.writeObject(o); byte[] bytes = bs.toByteArray(); String retStr = new String(bytes,"ISO-8859-1"); //按照ISO-8859-1解码 return retStr; }catch(Exception e){ e.printStackTrace(); return null; } }
//从字符串中读取出对象 public static Object derialzeFromString(String content){ ObjectInputStream ois = null; try{ byte[] bytes = content.getBytes("ISO-8859-1");//按照ISO-8859-1编码 ByteArrayInputStream bs = new ByteArrayInputStream(bytes); ois = new ObjectInputStream(bs); Object o = ois.readObject(); return o; }catch(Exception e){ e.printStackTrace(); return null; } }