在 Java J2ME 中使用 RecordStore

发布于 2024-08-25 16:51:16 字数 850 浏览 12 评论 0原文

我目前正在做一些J2ME开发。我遇到的问题是,用户可以向记录存储添加和删除元素,如果一条记录被删除,则该记录将保留为空,其他记录不会向上移动。我试图提出一个循环来检查记录中是否有任何内容(如果它已被删除),如果有,那么我想将该记录的内容添加到列表中。 我的代码类似于如下:

      for (int i = 1; i <= rs.getNumRecords(); i++)
      {
        // Re-allocate if necessary

        if (rs.getRecordSize(i) > recData.length)
          recData = new byte[rs.getRecordSize(i)];
        len = rs.getRecord(i, recData, 0);
        st = new String(recData, 0, len);
        System.out.println("Record #" + i + ": " + new String(recData, 0, len));
        System.out.println("------------------------------");
        if(st != null)
        {
            list.insert(i-1, st, null);
        }

      }

当到达 rs.getRecordSize(i) 时,我总是得到“javax.microedition.rms.InvalidRecordIDException:错误查找记录”。我知道这是因为记录为空,但我想不出解决这个问题的方法。

任何帮助将不胜感激。

提前致谢。

I am currently doing some J2ME development. I am having a problem in that a user can add and remove elements to the record store, and if a record gets deleted, then that record is left empty and the others don't move up one. I'm trying to come up with a loop that will check if a record has anything in it (incase it has been deleted) and if it does then I want to add the contents of that record to a list.
My code is similar to as follows:

      for (int i = 1; i <= rs.getNumRecords(); i++)
      {
        // Re-allocate if necessary

        if (rs.getRecordSize(i) > recData.length)
          recData = new byte[rs.getRecordSize(i)];
        len = rs.getRecord(i, recData, 0);
        st = new String(recData, 0, len);
        System.out.println("Record #" + i + ": " + new String(recData, 0, len));
        System.out.println("------------------------------");
        if(st != null)
        {
            list.insert(i-1, st, null);
        }

      }

When it gets to rs.getRecordSize(i), I always get a "javax.microedition.rms.InvalidRecordIDException: error finding record". I know this is due to the record being empty but I can't think of a way to get around this problem.

Any help would be much appreciated.

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

你没皮卡萌 2024-09-01 16:51:16

您应该使用 RecordEnumeration 来访问记录:

RecordEnumeration renum = rs.enumerateRecords(null, null, false);
while (renum.hasNextElement())
{
    int index = renum.nextRecordId();
    if (store.getRecordSize(index) == STORE_LEN)
    {

    }
}

您不能依赖 recordId 来获得任何有用的信息。使用不同的技术重新分配已删除的记录。

You should use a RecordEnumeration to visit the records:

RecordEnumeration renum = rs.enumerateRecords(null, null, false);
while (renum.hasNextElement())
{
    int index = renum.nextRecordId();
    if (store.getRecordSize(index) == STORE_LEN)
    {

    }
}

You can't rely on the recordId for anything useful. Use a different technique to reallocate a deleted record.

意犹 2024-09-01 16:51:16

为了让您的记录真正被删除,您必须关闭RecordStore。操作 RecordStore 的正确方法是打开它,使用它,最后关闭它。希望这对你有用

In order to have your records truly deleted you have to close de RecordStore. The correct way to operate a RecordStore is by opening it, using it an finally closing it. Hope this is usefull to you

凉城 2024-09-01 16:51:16

您可以尝试使用以下方法:

 /**
 * checks if record i is a valid records
 * @param i (recordId
 * @return true/false
 */
public boolean isValidRecord(int id) {

    try {
        recordStore.getRecordSize(id);
        return true;
    } catch (RecordStoreNotOpenException e1) {
        e1.printStackTrace();
        return false;
    } catch (InvalidRecordIDException e1) {
        //e1.printStackTrace(); //this printStackTrace is hidden on purpose: the error is in fact used to find out if the record id is valid or not.
        return false;
    } catch (RecordStoreException e1) {
        e1.printStackTrace();
        return false;
    }

}

You could try to use the following method:

 /**
 * checks if record i is a valid records
 * @param i (recordId
 * @return true/false
 */
public boolean isValidRecord(int id) {

    try {
        recordStore.getRecordSize(id);
        return true;
    } catch (RecordStoreNotOpenException e1) {
        e1.printStackTrace();
        return false;
    } catch (InvalidRecordIDException e1) {
        //e1.printStackTrace(); //this printStackTrace is hidden on purpose: the error is in fact used to find out if the record id is valid or not.
        return false;
    } catch (RecordStoreException e1) {
        e1.printStackTrace();
        return false;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文