如何使用代号一个存储? [英] How to use Codename one Storage?

查看:87
本文介绍了如何使用代号一个存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的LWUIT应用程序移植到代号one .

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

我在LWUIT中使用了RMS,现在显然我必须将其转换为Storage.

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

我不知道Storage类如何在Codename 1中工作,而Codename 1的文档对此一无所知.

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

1)存储文件的结构是什么?

1) What is the structure of a storage file?

->在J2ME RecordStore中,您像表一样将记录捆在一起.每行对应一个记录.每个记录都有一个唯一的记录ID,您可以使用该记录ID访问该记录.每个记录中都可以存储一些数据.

--> 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.

这如何映射到Storage类?

How does this map to Storage class?

2)我想在存储中存储一些记录,该怎么办?

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

文档说:

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

->在LWUIT中,它类似于Storage.init(storageName). ;但是,在代号one中没有init !!!如何使用代号"one"打开存储?

--> 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)如果我尝试打开一个不存在的存储文件,将会发生什么(RMS给出了例外)?

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

推荐答案

关于存储的最简单方法是将其视为平面文件系统(无目录/文件夹).

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

在RMS之上运行时,此文件系统抽象将无缝地映射到RMS数据库.

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

请注意,不再需要使用init()来存储代号为One的代码,在LWUIT下,它仅执行基本初始化,并且通常会忽略该名称.

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.

Storage类有几种方法:

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)

如果给定的存储文件存在,则返回true

Returns true if the given storage file exists

String[]    listEntries()

列出存储文件的名称

您可以使用它们来存储和检查数据是否存在.但是,您还可以通过以下两种方法在不使用输入/输出流的情况下将复杂的对象存储在存储中:

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)

从存储中读取对象,如果对象不存在,则返回null

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

因此,要模拟诸如byte []存储之类的内容,您可以执行以下操作:

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);

然后将其读取为:

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

这篇关于如何使用代号一个存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆