两个小时精通Android开发之数据持久存储篇
作者:孙东风 2010-01-15(转载务必注明出处)
笔者在前面的两篇文章《两个小时精通Android开发之界面篇》、《两个小时精通Android开发之按键映射篇》分别讲了无缝移植J2ME程序到Android平台上对界面和用户按键交互所做的适配接口,原则上利用这些接口原有的J2ME程序基本不用做任何的修改就可以运行在Android平台上,所以精通J2ME也就等于精通了Android。
笔者这篇文章里要讲述的是J2ME平台和Android平台另外一个重要的不同点,那就是数据持久存储系统。
J2ME平台里采用RMS系统进行数据的持久存储,而Android平台则提供了丰富的接口进行数据的持久存储,但任何持久存储的本质无非就是数据串行化后被保存到磁盘空间上,仔细研究J2ME平台RMS系统的实现源码可以看到,J2ME是通过一个叫做RecordStoreFile的类进行数据持久化存储的,而这个RecordStoreFile类的实现如下:
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
补充:移动开发 , Android ,