Two hours of data persistence storage proficient in Android Development

Source: Internet
Author: User

Author: Sun Dongfeng 2010-01-15 (source must be noted for reprinting)

 

In the previous two articles, "two-hour proficient Android development interface" and "two-hour proficient Android Development Key ing" respectively, the author talked about how to seamlessly port the agent to the Android platform. interface and user button interaction to adapt to the interface, in principle, using these interfaces, the original j2s program can run on the Android platform without any modifications. Therefore, proficient in j2s is equivalent to proficient in Android.

 

In this article, I want to talk about another important difference between the two platforms: the persistent data storage system.

 

The RMS system is used for persistent data storage, while the Android platform provides rich interfaces for persistent data storage, however, the essence of any persistent storage is that the data is serialized and saved to the disk space. You can view the source code of the RMS system on the j2_based platform, the implementation of the RecordStoreFile class is as follows:

 

Public class RecordStoreFile {

Private static SecurityToken classSecurityToken;

Private static final String dbExtension = ". db ";

Private RandomAccessStream recordStream;

Private String myStoragePath;

 

Public static void initSecurityToken (SecurityToken token ){

If (classSecurityToken! = Null ){

Return;

}

ClassSecurityToken = token;

}

 

Public RecordStoreFile (String uidPath)

Throws IOException

{

RandomAccessStream newStream;

MyStoragePath = uidPath;

 

NewStream = new RandomAccessStream (classSecurityToken );

NewStream. connect (myStoragePath, Connector. READ_WRITE );

RecordStream = newStream;

}

 

Public static String getUniqueIdPath (String fileName ){

Return getStoragePath (fileName );

}

Public static String getUniqueIdPath (String vendorName, String suiteName,

String fileName ){

Return getStoragePath (vendorName, suiteName, fileName );

}

 

Public static boolean exists (String uidPath ){

File file;

File = new File (classSecurityToken );

Return file. exists (uidPath );

}

 

Public static boolean deleteFile (String uidPath)

{

File file;

File = new File (classSecurityToken );

Try {

File. delete (uidPath );

Return true;

} Catch (IOException ioe ){

Return false;

}

}

Public void seek (int pos) throws IOException

{

RecordStream. setPosition (pos );

}

 

Public void write (byte [] buf) throws IOException

{

Write (buf, 0, buf. length );

}

Public void write (byte [] buf, int offset, int numBytes) throws IOException

{

RecordStream. writeBytes (buf, offset, numBytes );

}

Public int read (byte [] buf) throws IOException

{

Return read (buf, 0, buf. length );

}

 

Public int read (byte [] buf, int offset, int numBytes) throws IOException

{

Return recordStream. readBytes (buf, offset, numBytes );

}

Public void close () throws IOException

{

// Close recordStream if it exists

If (recordStream! = Null ){

RecordStream. disconnect ();

RecordStream = null;

}

}

 

Public void truncate (int size) throws IOException

{

If (recordStream! = Null ){

RecordStream. truncate (size );

}

}

 

Public static String [] listRecordStores (){

Return listRecordStoresForSuite (new File (classSecurityToken ),

GetStoragePath (null), false );

}

 

Private static String [] listRecordStoresForSuite (File storage,

String suiteStorageRoot,

Boolean rawNames ){

Vector files;

Vector names;

String file;

String asciiName;

Files = storage. filenamesThatStartWith (suiteStorageRoot );

Names = new Vector ();

// Work through list of strings from the directory

For (int I = 0; I <files. size (); I ++ ){

File = (String) files. elementAt (I );

If (file. endsWith (dbExtension )){

If (rawNames ){

Names. addElement (file );

} Else {

AsciiName = file. substring (suiteStorageRoot. length (),

File. length ()-3 );

Names. add

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.