Object to byte array and byte array to Object in Java
원출처 : http://scr4tchp4d.blogspot.com/2008/07/object-to-byte-array-and-byte-array-to.html
Here how to convert an Object to a byte array, and vice-versa.
요기에 객체를 바이트 배열로, 그 반대로 바꾸는 방법이 있습니다!
역시 구글링이랄까.
Here how to convert an Object to a byte array, and vice-versa.
요기에 객체를 바이트 배열로, 그 반대로 바꾸는 방법이 있습니다!
역시 구글링이랄까.
public byte[] toByteArray (Object obj)
{
byte[] bytes = null;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
ObjectOutputStream oos = new ObjectOutputStream(bos);
oos.writeObject(obj);
oos.flush();
oos.close();
bos.close();
bytes = bos.toByteArray ();
}
catch (IOException ex) {
//TODO: Handle the exception
}
return bytes;
}
public Object toObject (byte[] bytes)
{
Object obj = null;
try {
ByteArrayInputStream bis = new ByteArrayInputStream (bytes);
ObjectInputStream ois = new ObjectInputStream (bis);
obj = ois.readObject();
}
catch (IOException ex) {
//TODO: Handle the exception
}
catch (ClassNotFoundException ex) {
//TODO: Handle the exception
}
return obj;
}
'밤을 지새다 > Java' 카테고리의 다른 글
| Java StAX: XMLInputFactory (0) | 2012.04.30 |
|---|---|
| Java StAX (0) | 2012.04.30 |
| Exceptions (0) | 2011.09.05 |
| 자바. 얼마나 알고 사용하고 계신가요? (0) | 2011.08.25 |
| 메서드 체인화 기법 (0) | 2011.08.17 |