1. Overview
1.概述
In this short tutorial, we’ll learn how to convert a Java object to a byte array and vice versa.
在这个简短的教程中,我们将学习如何将一个Java对象转换为一个字节数组,反之亦然。
2. Use Plain Java
2.使用普通的Java
For example, suppose we have a User class:
例如,假设我们有一个User类。
public class User implements Serializable {
private String name;
@Override
public String toString() {
return "User{name=" + name + "}";
}
// getters and setters
}
We can use a ByteArrayOutputStream and ObjectOutputStream object to serializable an object to a byte array.
我们可以使用ByteArrayOutputStream和ObjectOutputStream对象来将一个对象序列化为一个字节数组。
Let’s not forget to use try-with-resources so that we don’t have to worry about closing streams:
我们不要忘记使用try-with-resources,这样我们就不必担心关闭流。
User user = new User();
user.setName("Josh");
try (ByteArrayOutputStream bos = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bos)) {
oos.writeObject(user);
}
Then we’ll use ByteArrayInputStream and ObjectInputStream to deserialize our received byte array to an object before finally casting it to User:
然后我们将使用ByteArrayInputStream和ObjectInputStream将我们收到的字节数组反序列化为一个对象,最后将其转换为User。
try (ByteArrayInputStream bis = new ByteArrayInputStream(data);
ObjectInputStream ois = new ObjectInputStream(bis)) {
User deserializedUser = (User) ois.readObject();
System.out.println(deserializedUser);
}
Note that our User class must implement the Serializable interface. Otherwise, the above code will throw a NotSerializableException.
注意,我们的User类必须实现Serializable接口。否则,上述代码将抛出一个NotSerializableException。
3. Use Apache Commons Lang
3.使用Apache Commons Lang
We can use the SerializationUtils class from the Apache Commons Lang library to achieve the same goal.
我们可以使用Apache Commons Lang库中的SerializationUtils类来实现同样的目标。
This class has a method named serialize(), which is used to serialize an object to a byte array:
这个类有一个名为serialize()的方法,用于将一个对象序列化为一个字节数组。
byte[] data = SerializationUtils.serialize(user);
And a deserialize() method to deserialize byte array to object:
还有一个deserialize()方法,将字节数组反序列化为对象。
User deserializedUser = SerializationUtils.deserialize(data);
The above methods have parameters of type Serializable. So, our User class still needs to implement the Serializable interface, just as it did in our plain Java example.
上述方法的参数类型为Serializable.因此,我们的User类仍然需要实现Serializable接口,就像在我们的普通Java示例中那样。
4. Use the SerializationUtils Class of Spring Framework
4.使用Spring框架的SerializationUtils类
Finally, if our project is already using Spring Framework, we can use the SerializationUtils class from the org.springframework.util package. The method names are the same as the ones in the Apache Commons Lang library.
最后,如果我们的项目已经在使用Spring框架,我们可以使用org.springframework.util包中的SerializationUtils类。这些方法的名称与Apache Commons Lang库中的方法相同。
First, we can serialize our User object to a byte array:
首先,我们可以将我们的User对象序列化为一个字节数组。
byte[] data = SerializationUtils.serialize(user);
And we can deserialize the result back to a User object:
我们可以将结果反序列化为一个User对象。
User deserializedUser = SerializationUtils.deserialize(data);
As usual, our User class must implement the Serializable interface or we’ll get a NotSerializableException when we run the above code.
像往常一样,我们的User类必须实现Serializable接口,否则我们在运行上述代码时将得到一个NotSerializableException。
5. Conclusion
5.总结
In summary, we’ve learned three different ways to convert a Java object to a byte array and vice versa. All of these methods require the input object to implement the Serializable interface to get the job done.
综上所述,我们已经学会了三种不同的方法来将一个Java对象转换成一个字节数组,反之亦然。所有这些方法都需要输入对象实现Serializable接口来完成工作。