⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 feb01_ericg.txt

📁 TechTips j2me的常用技巧. 网络功能
💻 TXT
📖 第 1 页 / 共 2 页
字号:
}
catch( RecordStoreException e ){
    // some other error
}

The first parameter of the openRecordStore method is the name of 
the record store, which must be no longer than 32 characters. The 
name must only be unique within the MIDlet suite (MIDlets outside 
the suite cannot open them). The second parameter indicates
whether to create the record store if it doesn't already exist, or 
to throw a RecordStoreNotFoundException instead. 
RecordStoreNotFoundException extends RecordStoreException, the root 
class for all RMS exceptions. A RecordStoreException is thrown if 
a record store can't be created because there is not enough space 
or because of some other internal error.

At any given time, a single instance of a RecordStore object 
exists for each record store. If two or more MIDlets in the same 
suite open the same record store, they both get a reference to
the same object instance.

After a record store has been opened, you can close it by calling 
the closeRecordStore method:

try {
    rs.closeRecordStore();
}
catch( RecordStoreNotOpenException e ){
    // already closed
}
catch( RecordStoreException e ){
    // some other error
}

The record store is not actually closed until you call the 
closeRecordStore method as many times as you called the 
openRecordStore method to open it.

You can add records to an open record store using the addRecord 
method:

byte[] data = new byte[2];

data[0] = 0;
data[1] = 1;

try {
    int id = rs.addRecord( data, 0, data.length );
}
catch( RecordStoreFullException e ){
    // no room left for more data
}
catch( RecordStoreNotOpenException e ){
    // store has been closed
}
catch( RecordStoreException e ){
    // general error
}

The value returned by addRecord is the unique identifier for the 
new record. The record identifiers (IDs) start at 1 and increase 
each time a record is added. Use the getNextRecordID method to 
obtain the record ID for the next record to be added.

You can delete a record by calling the deleteRecord method, 
which takes the record ID as its only parameter.

The getRecord method reads the contents of a particular record.  
There are two forms of method. One form takes the record ID as 
its only parameter and returns a byte array with the record data.
The second form fills an existing byte array with the record 
contents, as in:

byte[] data = new byte[100];
int    id = ....; // get the ID from somewhere

try {
    int numBytes = rs.getRecord( id, data, 0 );
}
catch( ArrayIndexOutOfBoundsException e ){
    // record too big for the array
}
catch( InvalidRecordIDException e ){
    // record doesn't exist
}
catch( RecordStoreNotOpenException e ){
    // store has been closed
} 
catch( RecordStoreException e ){
    // general error
}

The first parameter is the record ID, the second parameter is the 
byte array to copy the contents into, and the third parameter is
the offset at which to start the copying. The byte array is 
assumed to be long enough to hold the record contents (you can
use the getRecordSize method to obtain the size of a particular
record). Use the second form of getRecord whenever possible, this 
form uses less memory than the first form, and does not 
overextend the garbage collector -- something particularly 
important when enumerating through an entire record store. 

You can replace a particular record with the setRecord method:

byte[] data = new byte[3];
int    id = ....; // get the ID from somewhere

data[0] = 0;
data[1] = 1;
data[2] = 2;

try {
    rs.setRecord( id, data, 0, data.length );
}
catch( RecordStoreFullException e ){
    // no room left for more data
}
catch( InvalidRecordIDException e ){
    // record doesn't exist
}
catch( RecordStoreNotOpenException e ){
    // store has been closed
}
catch( RecordStoreException e ){
    // general error;
}

Note that there is no way to modify part of the data in a record. 
You can only replace it with a new byte array.

Now that you've learned the basics of how to create and use 
a record store, let's look at a fuller example. Here's a MIDlet 
that prints the contents of an arbitrary record store to an 
output stream. 

import java.io.*;
import javax.microedition.midlet.*;
import javax.microedition.rms.*;

public class TestStore extends MIDlet {

    static final String DBNAME = "mydata";

    public TestStore() {

        RecordStore rs = null;

        // Data is persistent across MIDlet invocations.
        // So, first clear out the old record store...

        try {
            RecordStore.deleteRecordStore( DBNAME );
        }
        catch( Exception e ){
            // ignore any errors...
        }

        // Now create a new one and dump each element out....

        try {
            rs = RecordStore.openRecordStore( DBNAME, true );

            byte[] data1 = "Here is the first record".getBytes();
            byte[] data2 = "And here is the second".getBytes();
            byte[] data3 = "And then the third".getBytes();

            data3[0] = 0;
            data3[data3.length-1] = (byte) -1;

            rs.addRecord( data1, 0, data1.length );
            rs.addRecord( data2, 0, data2.length );
            rs.addRecord( data3, 0, data3.length );

            dumpRecordStore( rs, System.out );

            rs.closeRecordStore();
        }
        catch( RecordStoreException e ){
            System.out.println( e );
        }

        notifyDestroyed();
    }

    public void dumpRecordStore( RecordStore rs, PrintStream out )
    {
        if( rs == null ) return;

        StringBuffer hexLine = new StringBuffer();
        StringBuffer charLine = new StringBuffer();

        try {
            int    lastID = rs.getNextRecordID();
            byte[] data = new byte[100];
            int    size;

            for( int i = 1; i < lastID; ++i ){
                try {
                    size = rs.getRecordSize( i );
                    if( size > data.length ){
                        data = new byte[ size * 2 ];
                    }

                    out.println( "Record " + i + " of size "
                                                        + size );

                    rs.getRecord( i, data, 0 );
                    dumpRecord( data, size, out, hexLine, 
                                                  charLine, 16 );

                    out.println( "" );
                }
                catch( InvalidRecordIDException e ){
                    continue;
                }
            }
        }
        catch( RecordStoreException e ){
            out.println( "Exception reading record store: " + e );
        }
    }
 
    private void dumpRecord( byte[] data, int size,
                             PrintStream out,
                             StringBuffer hexLine,
                             StringBuffer charLine,
                             int maxLen )
    {
        if( size == 0 ) return;

        hexLine.setLength( 0 );
        charLine.setLength( 0 );

        int count = 0;

        for( int i = 0; i < size; ++i ){
            char b = (char) ( data[i] & 0xFF );

            if( b < 0x10 ){
                hexLine.append( '0' );
            }

            hexLine.append( Integer.toHexString( b ) );
            hexLine.append( ' ' );

            if( ( b >= 32 && b <= 127 ) ||
                Character.isDigit( b ) ||
                Character.isLowerCase( b ) ||
                Character.isUpperCase( b ) ){
                charLine.append( (char) b );
            } else {
                charLine.append( '.' );
            }

            if( ++count >= maxLen || i == size-1 ){
                while( count++ < maxLen ){
                hexLine.append( "   " );
                }

                hexLine.append( ' ' );
                hexLine.append( charLine.toString() );

                out.println( hexLine.toString() );
  
                hexLine.setLength( 0 );
                charLine.setLength( 0 );
                count = 0;
            }
        }
    }

    public void destroyApp( boolean unconditional ) {
    }

    public void startApp() {
    }

    public void pauseApp() {
    }
}

When you run TestStore using the MIDP reference implementation,
you should see the following printed in your console window:

Record 1 of size 24
48 65 72 65 20 69 73 20 74 68 65 20 66 69 72 73  Here is the firs
74 20 72 65 63 6f 72 64                          t record

Record 2 of size 22
41 6e 64 20 68 65 72 65 20 69 73 20 74 68 65 20  And here is the
73 65 63 6f 6e 64                                second

Record 3 of size 18
00 6e 64 20 74 68 65 6e 20 74 68 65 20 74 68 69  And then the thi
72 ff                                            rd

.  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .

- NOTE

Sun respects your online time and privacy. The Java Developer 
Connection mailing lists are used for internal Sun Microsystems(tm) 
purposes only. You have received this email because you elected 
to subscribe. To unsubscribe, go to the Subscriptions page 
(http://developer.java.sun.com/subscription/), uncheck the 
appropriate checkbox, and click the Update button.


- SUBSCRIBE

To subscribe to a JDC newsletter mailing list, go to the 
Subscriptions page (http://developer.java.sun.com/subscription/), 
choose the newsletters you want to subscribe to, and click Update.


- FEEDBACK
Comments? Send your feedback on the J2ME Tech Tips to:

jdc-webmaster@sun.com


- ARCHIVES
You'll find the J2ME Tech Tips archives at:

http://java.sun.com/jdc/J2METechTips/index.html

- COPYRIGHT
Copyright 2001 Sun Microsystems, Inc. All rights reserved.
901 San Antonio Road, Palo Alto, California 94303 USA.

This document is protected by copyright. For more information, see:

http://java.sun.com/jdc/copyright.html


J2ME Tech Tips 
February 20, 2001








⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -