Package totalcross.io

Class ObjectPDBFile

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable

    public class ObjectPDBFile
    extends PDBFile
    An extension to PDBFile that allows storage and retrieval of objects that implement the Storable interface. Create an ObjectPDBFile and use the addObject() method on the objects you want to store. If you want a particular object you can use loadObjectAt() to load the stored details into an object or to search through all records call resetSearch() and then loop with nextObject() until it returns false. The example below shows an example of it's use with a PDBFile of identical data:

     ObjectPDBFile oc = new ObjectPDBFile("Test.DATA");
     MyObject obj = new MyObject();
     oc.resetSearch();
     while (oc.nextObject(obj))
     {
        // do something with obj
     }
     

    Here's an example using unknown data. The two sections of code save a vector containing a number of Lines, Circles, and Squares (all implementing Storable) in no particular order, then loads it back in again.

       // save data
       ObjectPDBFile oc=new ObjectPDBFile("Test.DATA",ObjectPDBFile.CREATE);
       for(int i=0,size=objs.getCount();i++)
         oc.addObject((Storable)objs.get(i));
       oc.close();
    
       // load data
       ObjectPDBFile oc=new ObjectPDBFile("Test.DATA");
       oc.registerClass(new Line());
       oc.registerClass(new Circle());
       oc.registerClass(new Square());
       objs=new Vector();
       oc.resetSearch();
       Storable obj;
       while ((obj=oc.nextObject())!=null)
       {
         objs.add(obj);
       }
       oc.close();