java - 如何在java中的文件中存储实现org.apache.geode.pdx.PdxSerializable的对象
问题描述
我有一个实现类org.apache.geode.pdx.PdxSerializable
,需要将它的对象存储在 java 的文件中。为了存储在文件中,对象需要是,Serializable
但是 PDXSerializable 类被用于在 gemfire 中存储数据,因此我们不能使用 Serializable 类。
解决方案
为什么不使用自定义对象序列化?这是我快速创建的一个示例;
private class Foo implements PdxSerializable {
private String bar;
private Integer baz;
public Foo(final String bar, final Integer baz) {
this.bar = bar;
this.baz = baz;
}
public String getBar() {
return bar;
}
public Integer getBaz() {
return baz;
}
public void toData(PdxWriter out) {
//your pdx stuff
}
public void fromData(PdxReader in) {
//your pdx work
}
}
//and a custom reader and writer
private void writeCustom(final Foo foo, final Path path) throws IOException {
try(ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(path.toFile()))) {
objectOutputStream.writeChars(foo.getBar());
objectOutputStream.writeInt(foo.getBaz());
}
}
private Foo readCustom(final Path path) throws IOException {
try(ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(path.toFile()))) {
String bar = objectInputStream.readUTF();
Integer baz = objectInputStream.readInt();
return new Foo(bar, baz);
}
}
自定义序列化 Oracle 文档 - https://www.oracle.com/technetwork/articles/java/javaserial-1536170.html
一个类似的问题,答案很好 - Java Custom Serialization
推荐阅读
- visual-studio - Visual Studio 设置项目(或 Wix)并添加主机条目
- flutter - 颤动:水平listView项目之间的空间
- maven - Gradle 缺少传递依赖
- excel - 如何导出具有动态工作表名称的工作表
- r - 房地产网页抓取 [r]
- events - JavaFX 获取大写锁定状态
- wpf - 检查 WPF Prism 对话框是否已打开
- android - Android Studio 4.1 中的 Gradel 构建问题
- nginx - 从 NGINX-RTMP 实时模式获取时的音视频延迟
- azure-powershell - 如何更新自动化帐户中的模块文件?