×

Discussion Board

Results 1 to 4 of 4
  1. #1
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    Question about storing data in RecordStores...

    Hey folks,

    I'm trying to implement a high score table in the game I'm about to finish for the Nokia 7210 model phone. I've been playing around with the RecordStore a little bit to get the data saving procedures down, but I have a question before I start bearing down on development. First of all, I need to be able to store a 5 character string in a record. I see that the addRecord function takes a 3rd parameter that contains the number of bytes of the data being sent. How do I get the size of the string variable in bytes? In C it's sizeof(), but I haven't found a comparable function in J2ME. Getting the bytes of the string itsself isn't a problem, I found the getBytes function within the String class. Can anyone lead me to the right function to get the number of bytes stored within a String object, or pretty much any other variable type?

    Thanks in advance,
    Dave

  2. #2
    Regular Contributor
    Join Date
    Mar 2003
    Posts
    77

    RE: Question about storing data in RecordStores...

    String str = new String("My String"
    byte[] bytes = str.getBytes();
    int len = bytes.length;

    There is no function as such in java to get the length of an array, you simply access the length member of the array.

    I hope this is what you were asking.

  3. #3
    Registered User
    Join Date
    Mar 2003
    Posts
    25

    RE: Question about storing data in RecordStores...

    Thanks, that's what I needed for that. I've reached another problem, however. When I try to read the data back from the RecordStore, it doesn't do anything with the data until the application is being closed. For example, right now I'm reading a string from a record and then I'm outputting it with System.out.println() but the line I'm printing doesn't show up in the buffer until the program is being shut down. Is there something that I'm not doing that would cause this problem?

    Thanks,
    Dave

  4. #4
    Guest

    RE: Question about storing data in RecordStores...

    Writing and reading data to/from a RecordStore can be done easy with a little help of ByteArrayStreams and DataStreams.

    // Writes a string to record store
    // and returns its recordID, where rs is the RecordStore
    public int writeString(RecordStore rs,String val)
    throws RecordStoreNotOpenException,
    RecordStoreException,
    RecordStoreFullException,
    IOException{
    ByteArrayOutputStream data = new ByteArrayOutputStream();
    DataOutputStream out = new DataOutputStream(data);
    out.writeUTF(val);
    int id = rs.addRecord(data.toByteArray(),0,data.size());
    out.close();
    out = null;
    data = null;
    return id;
    }

    // Returns a string from the record with
    // recordId as Id. Where rs is the RecordStore
    public String readString(RecordStore rs,int recordId)
    throws RecordStoreNotOpenException,
    InvalidRecordIDException,
    RecordStoreException,
    IOException{
    DataInputStream in = new DataInputStream(new ByteArrayInputStream(rs.getRecord(recordId)));
    String data = in.readUTF();
    in.close();
    in = null;
    return data;
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×