隨筆 - 6  文章 - 129  trackbacks - 0
          <2025年6月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          常用鏈接

          留言簿(14)

          隨筆檔案(6)

          文章分類(467)

          文章檔案(423)

          相冊

          收藏夾(18)

          JAVA

          搜索

          •  

          積分與排名

          • 積分 - 827235
          • 排名 - 49

          最新評論

          閱讀排行榜

          評論排行榜

          package serializable;

           

          import java.io.ByteArrayInputStream;

          import java.io.ByteArrayOutputStream;

          import java.io.ObjectInputStream;

          import java.io.ObjectOutputStream;

           

          public class ObjectToIO

          {

              /**

               *

               * 對象轉Byte數組

               *

               * @param obj

               *

               * @return

               *

               * @throws Exception

               *

               */

              public static byte[] objectToBytes(Object obj) throws Exception

              {

                  // logger.debug("objectToString called ");

                  ByteArrayOutputStream out = new ByteArrayOutputStream();

                  ObjectOutputStream sOut = new ObjectOutputStream(out);

                  sOut.writeObject(obj);

                  sOut.flush();

                  byte[] bytes = out.toByteArray();

                  // logger.debug(bytes.toString());

                  return bytes;

           

              }

           

              /**

               *

               * 字節數組轉對象

               * @param content

               *

               * @return

               *

               * @throws Exception

               *

               */

           

              public static Object bytesToObject(byte[] bytes) throws Exception

              {

                  // logger.debug("bytesToObject called ");

                  // byteobject

                  ByteArrayInputStream in = new ByteArrayInputStream(bytes);

                  ObjectInputStream sIn = new ObjectInputStream(in);

                  return sIn.readObject();

           

              }

          }

          轉自:http://wenku.baidu.com/view/1e0a23b465ce050876321373.html

          posted on 2011-09-16 10:51 Ke 閱讀(5342) 評論(0)  編輯  收藏 所屬分類: javawebservice
          主站蜘蛛池模板: 江北区| 太原市| 信宜市| 都匀市| 石屏县| 泰来县| 城步| 浙江省| 正定县| 田东县| 疏勒县| 许昌市| 县级市| 台北市| 曲周县| 巨鹿县| 阿拉尔市| 内黄县| 泉州市| 龙海市| 高阳县| 泽库县| 永善县| 长沙市| 嘉荫县| 巴马| 双鸭山市| 宝兴县| 鹤岗市| 永仁县| 屏边| 仙居县| 隆昌县| 沁水县| 伊吾县| 颍上县| 扶绥县| 榆中县| 肃南| 三都| 张家口市|