// byte array to Object
public void byteArr2Object(byte[] buf) {
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream (buf);
ObjectInputStream
ois = new ObjectInputStream (bis);
obj = ois.readObject();
ois.close();
bis.close();
} catch (IOException ex) {
ex.printStackTrace();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
}
return obj;
}
// object to byte array
public void objectToByteArr(Object o) {
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(o);
oos.flush();
bytes = bos.toByteArray ();
oos.close();
bos.close();
} catch (IOException ex) {
ex.printStackTrace();
}
return bytes;
}
【Java】对象、字节转换
最新推荐文章于 2024-03-30 01:46:10 发布