File 对象实例与Byte[]之间的转换

[color=red]主要的类是ObjectOutputStream ObjectInputStream 和 ByteArrayOutputStream ByteArrayInputStream [/color]

对象转Byte[]

ObjectOutputStream oos = null;
ByteArrayOutputStream byteOut = null;
try {
byteOut = new ByteArrayOutputStream();
oos = new ObjectOutputStream(byteOut);
oos.writeObject(obj);
byte[] bytes = byteOut.toByteArray();
} catch (Exception e) {
}

Byte[]转对象


ByteArrayInputStream in = null;
try {
in = new ByteArrayInputStream(bytes);
ObjectInputStream objIn = new ObjectInputStream(in);
Object obj = objIn.readObject();
} catch (Exception e) {
}

你可能感兴趣的:(File)