์๋ ํ์ธ์ ์ผ์ด์น์ ๋๋ค.
์ค๋์ ์๋ฐ ๊ฐ์ฒด๋ฅผ ๋ฐ์ดํธ ๋ฐฐ์ด๋ก ๋ณํํ๋ ๋ฐฉ๋ฒ์ ๋ํด์ ํฌ์คํ ํฉ๋๋ค.
1. Object to byte[]
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
byte[] yourBytes = bos.toByteArray();
...
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException ex) {
// ignore close exception
}
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
2. byte[] to Object
ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...
} finally {
try {
bis.close();
} catch (IOException ex) {
// ignore close exception
}
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
๊ธฐ๋ณธ์ ์ธ ์๋ฐ๋ฅผ ์ด์ฉํ๋ฉด ์ ๋ ๊ฒ ํ์๋ฉด ๋๋๋ฐ ์ํ์น ์ ํธ์ ์ด์ฉํ๋ฉด ์ข ๋ ํธํ๊ฒ ํ ์ ์์ต๋๋ค.
ApacheUtils ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ๋ค์ด ๋ฐ์์ ์ฌ์ฉํ์๋ฉด ๋ฉ๋๋ค.
To Serialize:
byte[] data = SerializationUtils.serialize(yourObject);
To Deserialize:
YourObject yourObject = (YourObject) SerializationUtils.deserialize(byte[] data)
๋ฌ๋ ํ์ค๋ก Object -> byte[] ๋ ๊ฐ๋ฅํ๊ณ ์ญ์ผ๋ก๋ ๊ฐ๋ฅํ๋ค์.
์ฐธ ๊ฐ๋จํ์ฅฌ์~?
์ด์ ์ผ์ด์น์์ต๋๋ค.
์ฆํํ์ธ์~
'๐ป Programming > Java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Java/์๋ฐ] ํ ์คํธ ํ์ผ ๋ด์ฉ ์ฝ์ด์ ์ถ๋ ฅํ๊ธฐ (0) | 2016.12.04 |
---|---|
ํ๋ก์ ํธ ํ์ฌ ๊ฒฝ๋ก ์ป๊ธฐ ( Getting current working directory ) (0) | 2016.06.15 |
JVM ํด๋์ค๋ก๋๋? ( What is ClassLoader ? ) (0) | 2015.06.18 |
[ Java / ์๋ฐ ] ์์คํ ๋ ๋ฆฝ์ ์ธ ๋ผ์ธ ๊ตฌ๋ถ์ (0) | 2015.04.27 |
[ Java / ์๋ฐ ] ํ์ฌ ๋ ์ง ๋ฐ ์๊ฐ ๊ตฌํ๊ธฐ (0) | 2015.04.27 |