How to use Codename one Storage?

扶醉桌前 提交于 2019-12-01 07:06:35

问题


I am trying to port my LWUIT application to Codename one.

I have used RMS in LWUIT and now obviously I have to transform this to Storage.

I don't understand how the Storage class works in Codename one and the documentation for codename one has nothing about either.

1) What is the structure of a storage file?

--> In J2ME RecordStore , you have records bunched together like a table. Every row, corresponds to a record. Each record has a unique record ID and you can access the record with this record id. Every record can have some data stored in it.

How does this map to Storage class?

2)I wish to store some records in my storage, how do i do it?

The documentation says:

static Storage  getInstance() 
          Returns the storage instance or null if the storage wasn't initialized using a call to init(String) first.

--> In LWUIT it was something like Storage.init(storageName). ; However there is no init in codename one!!!. How do I open a Storage in Codename one??

3)If i try to open a storage file which does not exist, what will happen (RMS gives an exception)?


回答1:


The easiest way to think about Storage is as a flat file system (without directories/folders).

When running on top of RMS this file system abstraction is mapped to the RMS database seamlessly for you.

Notice that init() for Storage in Codename One is no longer necessary, under LWUIT it only performed basic initialization and the name was usually ignored.

The Storage class has several methods:

InputStream createInputStream(String name)

Creates an input stream to the given storage source file

OutputStream    createOutputStream(String name)

Creates an output stream to the storage with the given name

boolean     exists(String name)

Returns true if the given storage file exists

String[]    listEntries()

Lists the names of the storage files

You can use these to just store and check if data exists. However you can also store complex objects in storage without using input/output streams by using these two methods:

 Object     readObject(String name)

Reads the object from the storage, returns null if the object isn't there

 boolean    writeObject(String name, Object o)

Writes the given object to storage assuming it is an externalizable type or one of the supported types

So to simulate something like byte[] storage you can do something like this:

Vector p = new Vector();
byte[] myData = ...;
p.addElement(myData);
p.addElement(additionalData);
Storage.getInstance().writeObject("myStore", p);

Then just read it as:

Vector p = (Vector)Storage.getInstance().read("myStore");
// p will be null if nothing was written


来源:https://stackoverflow.com/questions/11275975/how-to-use-codename-one-storage

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!