随笔 - 6  文章 - 129  trackbacks - 0
<2024年4月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011

常用链接

留言簿(14)

随笔档案(6)

文章分类(467)

文章档案(423)

相册

收藏夹(18)

JAVA

搜索

  •  

积分与排名

  • 积分 - 814910
  • 排名 - 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 阅读(5304) 评论(0)  编辑  收藏 所属分类: javawebservice

只有注册用户登录后才能发表评论。


网站导航: