概述
Java可序列化对象到字节数组
假设我有一个可序列化的类byte[]。
我想通过套接字将其作为byte[]传输到另一台机器,在那里从接收的字节重建它。
我怎么能实现这个目标?
6个解决方案
368 votes
准备要发送的字节:
ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = null;
try {
out = new ObjectOutputStream(bos);
out.writeObject(yourObject);
out.flush();
byte[] yourBytes = bos.toByteArray();
...
} finally {
try {
bos.close();
} catch (IOException ex) {
// ignore close exception
}
}
从字节创建对象:
ByteArrayInputStream bis = new ByteArrayInputStream(yourBytes);
ObjectInput in = null;
try {
in = new ObjectInputStream(bis);
Object o = in.readObject();
...
} finally {
try {
if (in != null) {
in.close();
}
} catch (IOException ex) {
// ignore close exception
}
}
Taylor Leese answered 2019-02-19T23:44:42Z
265 votes
最好的方法是使用Apache Commons Lang的SerializationUtils。
要序列化:
byte[] data = SerializationUtils.serialize(yourObject);
要反序列化:
YourObject yourObject = SerializationUtils.deserialize(data)
如上所述,这需要Commons Lang库。 它可以使用Gradle导入:
compile 'org.apache.commons:commons-lang3:3.5'
Maven的:
org.apache.commons
commons-lang3
3.5
Jar文件
这里提到了更多方法
或者,可以导入整个集合。 请参阅此链接
uris answered 2019-02-19T23:45:46Z
70 votes
如果您使用Java> = 7,则可以使用try with resources改进已接受的解决方案:
private byte[] convertToBytes(Object object) throws IOException {
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutput out = new ObjectOutputStream(bos)) {
out.writeObject(object);
return bos.toByteArray();
}
}
反过来说:
private Object convertFromBytes(byte[] bytes) throws IOException, ClassNotFoundException {
try (ByteArrayInputStream bis = new ByteArrayInputStream(bytes);
ObjectInput in = new ObjectInputStream(bis)) {
return in.readObject();
}
}
Víctor Romero answered 2019-02-19T23:46:17Z
3 votes
可以通过SerializeUtils,serialize& 通过ApacheUtils反序列化方法将对象转换为byte [],反之亦然,如@uris answer中所述。
通过序列化将对象转换为byte []:
byte[] data = SerializationUtils.serialize(object);
通过反序列化将byte []转换为对象::
Object object = (Object) SerializationUtils.deserialize(byte[] data)
单击下载org-apache-commons-lang.jar的链接
单击以集成.jar文件:
FileName - > 打开Medule设置 - > 选择你的模块 - > 依赖关系 - > 添加Jar文件,你就完成了。
希望这可以帮助。
Pankaj Lilan answered 2019-02-19T23:47:16Z
0 votes
我还建议使用SerializationUtils工具。 我想对@Abilash做出错误的评论。 toByteArray()方法不限于1024字节,这与此处的另一个答案相反。
public static byte[] serialize(Object object) {
if (object == null) {
return null;
}
ByteArrayOutputStream baos = new ByteArrayOutputStream(1024);
try {
ObjectOutputStream oos = new ObjectOutputStream(baos);
oos.writeObject(object);
oos.flush();
}
catch (IOException ex) {
throw new IllegalArgumentException("Failed to serialize object of type: " + object.getClass(), ex);
}
return baos.toByteArray();
}
乍一看,您可能认为toByteArray()只允许固定大小。 但是如果你仔细看看toString(),你会发现如有必要,流会增长:
此类实现数据所在的输出流 写入字节数组。 缓冲区自动增长为数据 是写的。 可以使用toByteArray()和检索数据 toString()。
gzg_55 answered 2019-02-19T23:47:56Z
0 votes
我想将它作为byte []通过套接字传输到另一台机器
// When you connect
ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
// When you want to send it
oos.writeObject(appMessage);
从收到的字节重建它。
// When you connect
ObjectInputStream ois = new ObjectInputStream(socket.getInputStream());
// When you want to receive it
AppMessage appMessage = (AppMessage)ois.readObject();
user207421 answered 2019-02-19T23:48:27Z
最后
以上就是默默烤鸡为你收集整理的java 字节序列_Java可序列化对象到字节数组的全部内容,希望文章能够帮你解决java 字节序列_Java可序列化对象到字节数组所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复