Problem: Getting the correct number of records from a shapefile


After a record is deleted from a shape file using 'com.esri.mo.file.shp.Shapefile.delRecord(int recNum)' method and 'getNumRecs()' method is called on the same object, the total number of records will remain same.

In MapObjects Java 2.0, Shapefile class is within com.esri.mo2.file.shp package.


Shapefile.delRecord(int rec) only flags the record as deleted but does not remove it permanently from the file. Shapefile.getNumRecs() method will return the number of records stored in the header of the shape file, which only stores the total number of records, including those flagged as deleted.

Solution or Workaround

Use for-loop to go through all the records in the shapefile and count only those that return the status as OK as shown in the following example:

int rec=0;
for(int i=0;i<shapefile.getNumRecs();i++){
int isOK=shapefile.getRecord();
if (isOK==Shapefile.OK)rec++;
//total # of records = rec