ITeye博客

java中对像与字节数据之间的转化

falchion · 2008-03-28 16:19 · java · Markdown

1、对像变成字节数组

   ByteArrayOutputStream   buffers   =   new   ByteArrayOutputStream();   
        try{   
            ObjectOutputStream   out   =   new   ObjectOutputStream(buffers);   
            out.writeObject(对像变量);   
            out.close();   
        }catch(Exception   e){   
                System.err.print(e.getMessage());   
                return   null;   
        }   
        return   buffers.toByteArray();//返回对像的字节数组

2、字节数组转化为对像

  data[]=.....   
       对像的类型   obj;   
        try{   
            ByteArrayInputStream   buffers   =   new   ByteArrayInputStream(data);   
            ObjectInputStream   in   =   new   ObjectInputStream(buffers);   
            obj=(对像的类型)   in.readObject();      
            in.close();   
        }catch(Exception   e){   
                System.err.print(e.getMessage());   
        }   
  

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。