代码之家  ›  专栏  ›  技术社区  ›  KCK

如何在Java中实现Orj.ApACH.GeDe.PDX.PDXSerialI化的对象存储

  •  3
  • KCK  · 技术社区  · 5 年前

    我有一个课程实施 org.apache.geode.pdx.PdxSerializable 并且需要在Java中将文件的对象存储在文件中。对于文件中的sotring,对象需要 Serializable 但是类pdxserializable正用于在gemfire中存储数据,因此我们不能使用可序列化类。

    2 回复  |  直到 5 年前
        1
  •  3
  •   Laksitha Ranasingha    5 年前

    
     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);
            }
        }
    

    https://www.oracle.com/technetwork/articles/java/javaserial-1536170.html

    Java Custom Serialization