两个小时精通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[
相关新闻>>
- 发表评论
-
- 最新评论 更多>>