×

Discussion Board

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

    Using protocol file:// with object connector

    Hello everybody, I need to store some information in the phone in a persistent way (I want to get information even when I close the midlet and then I run it again). I used the following code:
    OutputConnection connOut = (OutputConnection)Connector.open("file://dir.dat", Connector.WRITE);
    DataOutputStream dos = connOut.openDataOutputStream();
    dos.writeUTF("trwetwet"
    dos.flush();
    dos.close();
    connOut.close();

    but it doesn't work on Nokia because it seems that nokia KVM cannot manage file protocol.

    How can I store information in a persistent way in the J2ME environment on a NOKIA phone (i.e. 7650)?

    Thank you very much for your help.

  2. #2
    Nokia Developer Expert
    Join Date
    Mar 2003
    Posts
    382

    RE: Using protocol file:// with object connector

    Hello

    There is a way to store data into Java Mobile by using RMS (record management system): => import javax.microedition.rms.*;



    private RecordStore rs = null;
    static final String REC_STORE = "db";

    public ReadWrite()
    {
    openRecStore(); // Create the record store

    // Write a few records and read them back
    writeRecord("recordstore example);
    writeRecord("test data"
    readRecords();

    closeRecStore(); // Close record store
    deleteRecStore(); // Remove the record store
    }

    public void openRecStore()
    {
    try
    {
    // The second parameter indicates that the record store
    // should be created if it does not exist
    rs = RecordStore.openRecordStore(REC_STORE, true );
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }

    public void closeRecStore()
    {
    try
    {
    rs.closeRecordStore();
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }

    public void deleteRecStore()
    {
    if (RecordStore.listRecordStores() != null)
    {
    try
    {
    RecordStore.deleteRecordStore(REC_STORE);
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }
    }

    public void writeRecord(String str)
    {
    byte[] rec = str.getBytes();

    try
    {
    rs.addRecord(rec, 0, rec.length);
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }

    public void readRecords()
    {
    try
    {
    // Intentionally make this too small to test code below
    byte[] recData = new byte[5];
    int len;

    for (int i = 1; i <= rs.getNumRecords(); i++)
    {
    if (rs.getRecordSize(i) > recData.length)
    recData = new byte[rs.getRecordSize(i)];

    len = rs.getRecord(i, recData, 0);
    System.out.println("Record #" + i + ": " + new String(recData, 0, len));
    System.out.println("------------------------------"
    }
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }



    private void db(String str)
    {
    System.err.println("Msg: " + str);
    }
    }


    **********************************************************************
    CASE II
    **********************************************************************
    More better way is to use data streams when writing more that one data into record and when each data has different type (int, String..)


    private RecordStore rs = null; // Record store
    static final String REC_STORE = "db1"; // Name of record store

    public ReadWriteStreams()
    {
    openRecStore(); // Create the record store

    writeTestData(); // Write a series of records
    readStream(); // Read back the records

    closeRecStore(); // Close record store
    deleteRecStore(); // Remove the record store
    }



    public void openRecStore()
    {
    try
    {
    // The second parameter indicates that the record store
    // should be created if it does not exist
    rs = RecordStore.openRecordStore(REC_STORE, true );
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }

    public void closeRecStore()
    {
    try
    {
    rs.closeRecordStore();
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }

    public void deleteRecStore()
    {
    if (RecordStore.listRecordStores() != null)
    {
    try
    {
    RecordStore.deleteRecordStore(REC_STORE);
    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }
    }

    /*--------------------------------------------------
    * Create three arrays to write to record store
    *-------------------------------------------------*/

    public void writeTestData()
    {
    String[] strings = {"Test data one", "Test data two"};
    boolean[] booleans = {true, false};
    int[] integers = {4 , 5};

    writeStream(strings, booleans, integers);
    }

    /*--------------------------------------------------
    * Write to record store using streams.
    *-------------------------------------------------*/

    public void writeStream(String[] sData, boolean[] bData, int[] iData)
    {
    try
    {
    // Write data into an internal byte array
    ByteArrayOutputStream strmBytes = new ByteArrayOutputStream();

    // Write Java data types into the above byte array
    DataOutputStream strmDataType = new DataOutputStream(strmBytes);

    byte[] record;

    for (int i = 0; i < sData.length; i++)
    {
    // Write Java data types
    strmDataType.writeUTF(sData[i]);
    strmDataType.writeBoolean(bData[i]);
    strmDataType.writeInt(iData[i]);

    // Clear any buffered data
    strmDataType.flush();

    // Get stream data into byte array and write record
    record = strmBytes.toByteArray();
    rs.addRecord(record, 0, record.length);

    // Toss any data in the internal array so writes
    // starts at beginning (of the internal array)
    strmBytes.reset();
    }

    strmBytes.close();
    strmDataType.close();

    }
    catch (Exception e)
    {
    db(e.toString());
    }
    }

    /*--------------------------------------------------
    * Read from the record store using streams
    *-------------------------------------------------*/
    public void readStream()
    {
    try
    {
    // Careful: Make sure this is big enough!
    // Better yet, test and reallocate if necessary
    byte[] recData = new byte[50];

    // Read from the specified byte array
    ByteArrayInputStream strmBytes = new ByteArrayInputStream(recData);

    // Read Java data types from the above byte array
    DataInputStream strmDataType = new DataInputStream(strmBytes);

    for (int i = 1; i <= rs.getNumRecords(); i++)
    {
    // Get data into the byte array
    rs.getRecord(i, recData, 0);

    // Read back the data types
    System.out.println("Record #" + i);
    System.out.println("UTF: " + strmDataType.readUTF());
    System.out.println("Boolean: " + strmDataType.readBoolean());
    System.out.println("Int: " + strmDataType.readInt());
    System.out.println("--------------------"

    // Reset so read starts at beginning of array
    strmBytes.reset();
    }

    strmBytes.close();
    strmDataType.close();

    }
    catch (Exception e)
    {
    db(e.toString());

    }
    }

    private void db(String str)
    {
    System.err.println("Msg: " + str);
    }
    }


    I hope this will help you

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

    RE: Using protocol file:// with object connector

    Hi,

    for small data sizes the preferred way seems to be to use the quite comfortable javax.microedition.rms.* classes. I believe it is suitable for a couple of tens of KB.

    Cheers
    Stefan

Posting Permissions

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