2013-04-15 1 views
0

JSI (Java Spatial Index, RTree)을 사용하여 2D 공간 검색을 구현합니다. 아래의 코드를 사용하여 java.io.NotSerializableException을 트리거 한 파일을 트리에 저장하고 싶습니다.Java에서 파일로 /부터 직렬화 할 수없는 객체를 쓰거나 읽는 중

public class GeographicIndexer { 
    private static final Logger log = LoggerFactory.getLogger(GeographicIndexer.class); 
    public SpatialIndex spatialIndex = null; 

    public void init() { 
     this.spatialIndex = new RTree(); 
     this.spatialIndex.init(null); 
    } 

    public void add(float x1, float y1, float x2, float y2, int id) { 
     Rectangle rect = new Rectangle(x1, y1, x2, y2); 
     this.spatialIndex.add(rect, id); 
    } 

    public void add(float x, float y, int id) { 
     this.add(x, y, x, y, id); 
    } 

    public void saveIndex(String indexStorePath) { 
     try { 
      OutputStream file = new FileOutputStream(indexStorePath); 
      OutputStream buffer = new BufferedOutputStream(file); 
      ObjectOutput output = new ObjectOutputStream(buffer);  
      try { 
       output.writeObject(this.spatialIndex); 
      } finally { 
       output.close(); 
      } 
     } catch(IOException e) { 
      log.error("Fail to write geographic index"); 
      e.printStackTrace(); 
     } 
    } 

    public GeographicIndexer loadIndex(String indexStorePath) { 
     try { 
      InputStream file = new FileInputStream(indexStorePath); 
      InputStream buffer = new BufferedInputStream(file); 
      ObjectInput input = new ObjectInputStream(buffer); 

      try { 
       this.spatialIndex = (SpatialIndex)input.readObject(); 
      } catch (ClassNotFoundException e) { 
       log.error("Fail to read geographic index"); 
      } finally { 
       input.close(); 
      } 

      return this; 
     } catch(IOException e) { 
      log.error("Fail to read geographic index"); 
      return this; 
     } 
    } 
} 

이 서드 파티 클래스를 읽고 쓸 수 있도록 어떻게 직렬화합니까? 감사합니다. .

+0

아마 클래스는'Serializable'을 구현하지 않습니다! – NINCOMPOOP

+0

'output.writeObject (this.spatialIndex);'다음에 예외를 catch하고 전체 추적을 인쇄 할 수 있습니까? 'try'다음에 'finally'가 표시됩니다. – sanbhat

+0

@noob 그렇지 않습니다. 파일로 저장할 수 없다는 의미입니까? –

답변

1

com.infomatiq.jsi.rtree.RTreeSerializable을 구현하지 않으므로 Java 직렬화를 사용하여 해당 객체의 상태를 유지할 수 없습니다. one here과 같은 직렬화를위한 다른 프레임 워크를 사용할 수 있습니다.

-1

확장 해보십시오. 확장 클래스를 직렬화 가능하게 만드십시오. 그러면 파일에 쓸 수 있어야합니다.

+0

클래스를 만드는 것은 수퍼 클래스를 확장하는 것만으로도 수퍼 클래스를 직렬화하는 데 도움이되지 않습니다. 우리는 하위 클래스의 모든 변수를 복사 한 다음 직렬화해야합니다. – sanbhat

1

RTree는 Serializable을 구현하지 않으므로 Java Serialization을 사용할 수 없습니다.

+0

감사! 그런 객체를 쓰거나 읽는 것을 구현하는 방법을 권해 주시겠습니까? –